如何让@JsonIgnore工作我有一堂课。即使我把注释放在那里它对输出也没有影响。我正在使用杰克逊。
public class QuestionBlock implements ComparableByID{
int ID;
String title;
String description;
boolean deleted;
boolean isDraft;
boolean visible;
Timestamp modifiedDate;
String modifiedBy;
private List<Question> questions = new ArrayList<>();
@JsonIgnore
private List<Survey> surveys = new ArrayList<>();
...
@JsonIgnore
public List<Survey> getSurveys() {
return surveys;
}
@JsonIgnore
public void setSurveys(List<Survey> surveys) {
this.surveys = surveys;
}
}
这是我的控制器方法:
@RequestMapping(value = "/questionBlock/{id}",produces = "application/json;charset=UTF-8")
@ResponseBody
public QuestionBlock getQuestionBlock(@PathVariable("id") int id) {
return surveyService.getQuestionBlock(id);
}
这是我的servlet-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources location="/, classpath:/META-INF/web-resources/"
mapping="/resources/**" />
<context:property-placeholder location="classpath*:META-INF/*.properties" />
<context:component-scan base-package="com.adam.czibere" />
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="myDataSource" name="dataSource">
<property name="driverClassName" value="${database.driverClassName}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="true" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="1800000" />
<property name="numTestsPerEvictionRun" value="3" />
<property name="minEvictableIdleTimeMillis" value="1800000" />
<property name="validationQuery" value="SELECT 1" />
</bean>
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan">
<array>
<value>com.adam.czibere</value>
</array>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="json" value="application/json" />
</map>
</property>
<property name="viewResolvers">
<list>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="prefixJson" value="false" />
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</list>
</property>
</bean>
<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="prefixJson" value="false" />
<property name="supportedMediaTypes" value="application/json" />
</bean>
</list>
</property>
</bean>
</beans>
答案 0 :(得分:67)
我终于找到了解决方案。我从
更改了import语句import org.fasterxml.jackson.annotate.JsonIgnore;
到
import org.codehaus.jackson.annotate.JsonIgnore;
基本上你必须确保你到处使用同一个班级。
答案 1 :(得分:4)
注释应仅在'get'方法上。你似乎在你的私人领域有@Json ...注释。
答案 2 :(得分:3)
如果您正在使用Jackson的实现并且其注释不起作用,那可能是因为您有更好的优先级的杰克逊的另一个依赖。因此,如果你想确保杰克逊普遍存在的某些实现(恕我直言,最好的选择是你已经注释过的所有类的一个,因为可能它带有其他依赖项)在应用程序模块的pom中指定这种依赖。因此,如果您在多个模块中使用
注释所有实体import org.fasterxml.jackson.annotate.JsonIgnore;
不是替换所有导入,只需在应用程序pom中指定相应的依赖项:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
这将向Spring Boot澄清这是您要使用的实现。
答案 3 :(得分:2)
将 @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
放在字段声明之前为我解决了问题。
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private List<Survey> surveys = new ArrayList<>();
我使用的是 jackson 版本 2.11.3
答案 4 :(得分:0)
您需要更改org.codehaus.jackson版本。 我目前正在使用@JsonIgonre的1.1.1版本 不管用。 我用1.9.13更改了它,然后@JsonIgnore工作正常。
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.1.1</version>
</dependency>
Change to
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
Java代码是: -
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.codehaus.jackson.annotate.JsonIgnore;
@Entity
@Table(name="users")
public class Users implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String username;
private String firstname;
@JsonIgnore
private String lastname;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
答案 5 :(得分:0)
我在使用spring和hibernate时遇到了同样的问题。原因是在实体类中我使用的是org.codehaus.jackson.JsonIgnore,而在春天我使用的是com.fasterxml.jackson.jaxrs.JsonIgnore。修复两个层中的任何一个库,问题将消失。
答案 6 :(得分:0)
我在项目中遇到了这个问题,最后我找到了解决方案。
替换依赖项...
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
导入:com.fasterxml.jackson.annotation.JsonIgnore;
我希望,它将成功。
答案 7 :(得分:0)
就我而言,JsonIgnore并非仅在日期上工作。然后我知道那是因为我使用@JsonFormat()定义日期格式。删除即可解决问题。
答案 8 :(得分:0)
我为boolean添加了@Getter(onMethod _ = @ JsonIgnore)。
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class TestClass {
@JsonIgnore
private String name;
@Getter(onMethod_=@JsonIgnore)
private boolean isValid;
}