可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm working with RestEasy, Jboss 7 and EJB 3.1. I'm creating a RESTful web service that returns data in JSON format.
The problem is that I have a @ManyToOne
relationship on one of my entities which causes an infinite recursion during serialization. I tried using Jackson's @JsonIgnore
and @JsonBackReference
annotations to fix the problem but it seems as if they are being totally ignored and the infinite recursion is still occurring.
This is my User Class:
class User { private String userId; private Role role; @Id @Column(name = "\"UserId\"", unique = true, nullable = false, length = 30) public String getUserId() { return this.UserId; } public void setUserId(String UserId) { this.UserId = UserId; } @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "\"RoleId\"", nullable = false) //I have tried @JsonIgnore without @JsonBackReference @JsonIgnore //I have tried @JsonBackReference without @JsonIgnore //Also I have tried @JsonBackReference and @JsonIgnore together @JsonBackReference("role-User") public Role getRole() { return this.role; } }
This is a part of my Role Class:
@JsonManagedReference("role-User") @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "role") public Set getUsers() { return this.users; }
I have read somewhere that I should register Jackson with my application to be able to use regular Jaxb annotation so I created a class
@Provider @Produces(MediaType.APPLICATION_JSON) public class JacksonContextResolver implements ContextResolver { public JacksonContextResolver() { ObjectMapper mapper = new ObjectMapper(); AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(); mapper.getDeserializationConfig().setAnnotationIntrospector( introspector); mapper.getSerializationConfig().setAnnotationIntrospector(introspector); } public ObjectMapper getContext(Class> objectType) { return objectMapper; } }
The problem with the above being that JaxbAnnotationIntrospector()
is deprecated.
Please:
- Do you have any idea why the Jackson annotations are being ignored?
- How can I use the regular JAXB XML annotations instead of Jackson's?
- What can I use instead of
JaxbAnnotationIntrospector()
?
An answer to any of these question is appreciated, thanks.
Update:
For now I have excluded resteasy-jackson-provider
using jboss-deployment-structure.xml
and I am using Jettison instead. I still want to know how could I use Jackson!
回答1:
use import com.fasterxml.jackson.annotation.JsonBackReference;
instead of import org.codehaus.jackson.annotate.JsonBackReference;
.
It was the problem for me.
回答2:
I had this problem and got the same code to work by updating the version of the Jackson library in my build (pom.xml) from 1.8.x to 1.9.13. If you are using maven, edit your pom.xml to contain:
org.codehaus.jacksonjackson-mapper-asl1.9.13
Documentation doesn't help, but it seems that back references for Sets were not supported in the 1.8.x versions.
回答3:
Look, I have something similar to you and is working just fine for me...
@JsonBackReference("promotion-travel") @ManyToOne(cascade = CascadeType.ALL) @JoinTable(name="PROMOTION_TRAVEL_REL", joinColumns = @JoinColumn(name="TRAVEL_ID"), inverseJoinColumns = @JoinColumn(name="PROMOTION_ID") ) public Promotion getPromotion(){...
And this is what I have on Entity1
@JsonManagedReference("promotion-travel") @OneToMany(mappedBy="promotion") public List getTravelList(){...
I don't know if moving the annotation to the top will change anything, but that's the only thing that I can see...
Cheers,
回答4:
The problem here seems to be related to using Set
instead of List
. I had exactly the same problem and changing from Set
to List
fixed this, otherwise I always got Infinite recursion
error from Jackson. I don't know if this is really a bug in Jackson or do you have to provide some other annotations etc. when using Set
.
回答5:
i'm using spring 4.0.1 , hibernate 4.3.5 ,jackson 1.9.2 and STS IDE
i had the same exception but solved by annotating the setter by @Transient
idont know why but it works fine
This is my entity classes User.class
//i get that suggestion from some sites @JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" }) @Entity @Table(name = "user", catalog = "someSchema") public class User implements java.io.Serializable { private String name; private String password; private String username; private Set telephones = new HashSet(0); @OneToMany(fetch = FetchType.LAZY, mappedBy = "user") public Set getTelephones() { return this.telephones; } public void setTelephones(Set telephones) { this.telephones = telephones; } }
Telephone.class
@Entity @Table(name = "telephone", catalog = "someSchema") public class Telephone implements java.io.Serializable { private User user; private String telephone; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id", nullable = false) public User getUser() { return this.user; } @Transient public void setUser(User user) { this.user = user; } }
concerning registering jackson to my application, i used xml config
and mapper class
public class HibernateAwareObjectMapper extends ObjectMapper { public HibernateAwareObjectMapper() { Hibernate4Module hm = new Hibernate4Module(); registerModule(hm); } }