Java Spring @JsonIgnore 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19894955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring @JsonIgnore not working
提问by czadam
How can I get @JsonIgnore to work I have a class. And even if I put the annotation there it has no effect on the output. I am using Hymanson.
我怎样才能让@JsonIgnore 工作,我有一个班级。即使我将注释放在那里,它对输出也没有影响。我正在使用Hyman逊。
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;
}
}
This is my Controller method:
这是我的控制器方法:
@RequestMapping(value = "/questionBlock/{id}",produces = "application/json;charset=UTF-8")
@ResponseBody
public QuestionBlock getQuestionBlock(@PathVariable("id") int id) {
return surveyService.getQuestionBlock(id);
}
Here is my servlet-context.xml:
这是我的 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.MappingHymansonJsonView">
<property name="prefixJson" value="false" />
<property name="objectMapper" ref="HymansonObjectMapper" />
</bean>
</list>
</property>
</bean>
<bean id="HymansonObjectMapper" class="org.codehaus.Hymanson.map.ObjectMapper" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverter">
<property name="prefixJson" value="false" />
<property name="supportedMediaTypes" value="application/json" />
</bean>
</list>
</property>
</bean>
</beans>
采纳答案by czadam
I have finally found a solution. I changed the import statement from
我终于找到了解决办法。我将导入语句从
import com.fasterxml.Hymanson.annotate.JsonIgnore; // com. instead of org.
to
到
import org.codehaus.Hymanson.annotate.JsonIgnore;
Basically you have to make sure you are using the same class everywhere.
基本上,您必须确保在任何地方都使用相同的类。
回答by Steve
The annotation should only be on the 'get' methods. You seem to have @Json... annotations on your private fields.
注释应该只在“get”方法上。您的私有字段上似乎有 @Json... 注释。
回答by Pankaj Verma
You Need to change your org.codehaus.Hymanson version. I am currently using 1.1.1 version in which @JsonIgonre is not working. I changed it with 1.9.13 then @JsonIgnore is working fine.
您需要更改 org.codehaus.Hymanson 版本。我目前使用的是 1.1.1 版本,其中 @JsonIgonre 不起作用。我用 1.9.13 更改了它,然后 @JsonIgnore 工作正常。
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-jaxrs</artifactId>
<version>1.1.1</version>
</dependency>
Change to
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
And Java Code is :-
而 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.Hymanson.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;
}
}
回答by tanson
I faced the same issue while working with spring and hibernate. The reason for this was that in entity class I was using org.codehaus.Hymanson.JsonIgnore and in spring I was using com.fasterxml.Hymanson.jaxrs.JsonIgnore. Fix to any one library in both layers and problem will disappear.
我在使用 spring 和 hibernate 时遇到了同样的问题。这样做的原因是在实体类中我使用了 org.codehaus.Hymanson.JsonIgnore,而在 spring 中我使用了 com.fasterxml.Hymanson.jaxrs.JsonIgnore。修复两个图层中的任何一个库,问题就会消失。
回答by EliuX
If you are using an implementation of Hymanson and its annotations are not working it's probably because you have another dependency of Hymanson with better precedence. Hence if you want to assure that certain implementation of Hymanson prevales (IMHO the best choice is the one that you have all classes already annotated with, because probably it came with other dependencies) specify this dependency in the pom of the application module. So if you have in multiple modules all your entities annotated with
如果您正在使用 Hymanson 的实现并且其注释不起作用,则可能是因为您有另一个优先级更高的 Hymanson 依赖项。因此,如果您想确保 Hymanson prevales 的某些实现(恕我直言,最好的选择是您已经注释了所有类,因为它可能带有其他依赖项)在应用程序模块的 pom 中指定此依赖项。因此,如果您在多个模块中有所有实体注释
import com.fasterxml.Hymanson.annotate.JsonIgnore; // note: com. instead of org.
Instead of replacing all imports just specify the corresponding dependency in the application pom:
无需替换所有导入,只需在应用程序 pom 中指定相应的依赖项:
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
</dependency>
This will clarify to Spring Boot that this is the implementation you want to use.
这将向 Spring Boot 澄清这是您要使用的实现。
回答by RiTesh SrivasTav
I faced this issue in my project and finally I got the solution.
我在我的项目中遇到了这个问题,最后我得到了解决方案。
Replace the dependency...
替换依赖...
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
Import : com.fasterxml.Hymanson.annotation.JsonIgnore;
导入:com.fasterxml.Hymanson.annotation.JsonIgnore;
I hope, It will work.
我希望,它会起作用。
回答by Nitin Pareek
In my case JsonIgnore was not working only on dates. Then I understood that was because of @JsonFormat() that I was using to define the format of date. Removing that solved the issue.
就我而言, JsonIgnore 不仅仅适用于日期。然后我明白这是因为我用来定义日期格式的@JsonFormat()。删除解决了这个问题。