我怎么才能让@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>
发布于 2013-11-11 05:45:37
我终于找到了一个解决方案。我将import语句从
import com.fasterxml.jackson.annotate.JsonIgnore; // com. instead of org.
至
import org.codehaus.jackson.annotate.JsonIgnore;
基本上,你必须确保你在任何地方都使用相同的类。
发布于 2013-11-11 05:14:34
批注应该只在“get”方法上。你好像有@Json..。私有字段上的注释。
发布于 2017-04-28 09:58:10
如果你正在使用Jackson的一个实现,但它的注解不起作用,这可能是因为你有另一个更好优先级的jackson依赖项。因此,如果你想确保jackson prevales的某些实现(我认为最好的选择是你已经注释了所有类的实现,因为它可能带有其他依赖项),请在应用程序模块的pom中指定这个依赖项。因此,如果在多个模块中,所有实体都用
import com.fasterxml.jackson.annotate.JsonIgnore; // note: com. instead of org.
不需要替换所有的导入,只需在应用程序pom中指定相应的依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
这将向Spring Boot澄清这就是您想要使用的实现。
https://stackoverflow.com/questions/19894955
复制相似问题