@JsonIgnore and @JsonBackReference are ignored

I work with RestEasy, Jboss 7, and EJB 3.1. I am creating a RESTful web service that returns data in JSON format.

The problem is that I have a @ManyToOne relation on one of my entities that causes infinite recursion during serialization. I tried using the Jackson @JsonIgnore and @JsonBackReference to fix the problem, but it seems like they are completely ignored and infinite recursion is still happening.

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 part of my role class:

 @JsonManagedReference("role-User") @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "role") public Set<User> getUsers() { return this.users; } 

I read somewhere that I have to register Jackson with my application in order to be able to use regular Jaxb annotation, so I created a class

 @Provider @Produces(MediaType.APPLICATION_JSON) public class JacksonContextResolver implements ContextResolver<ObjectMapper> { 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 is that JaxbAnnotationIntrospector() deprecated.

You are welcome:

  • Do you have an idea why Jackson annotations are ignored?
  • How can I use regular JAXB XML annotations instead of Jackson's?
  • What can I use instead of JaxbAnnotationIntrospector() ?

The answer to any of these questions is welcome, thanks.

Update:

Now I have excluded resteasy-jackson-provider with jboss-deployment-structure.xml and instead I use Jettison. I still want to know how I can use Jackson!

+6
source share
5 answers

use import com.fasterxml.jackson.annotation.JsonBackReference; instead of import org.codehaus.jackson.annotate.JsonBackReference; .

This was a problem for me.

+4
source

I had this problem and I got the same code by updating the version of Jackson library in my assembly (pom.xml) from 1.8.x to 1.9.13. If you are using maven, edit your pom.xml so that it contains:

  <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> 

The documentation does not help, but it seems that backlinks for Sets were not supported in versions 1.8.x.

+3
source

Listen, I have something similar to you and works great 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<Travel> getTravelList(){... 

I don’t know if moving the annotation from above will change anything, but this is the only thing I see ...

Greetings

+2
source

The problem here seems to be related to using Set<User> instead of List<User> . I had exactly the same problem and changed it from Set<User> to List<User> , otherwise I always got an Infinite recursion error from Jackson. I don't know if this is really a bug in Jackson or if you need to provide some other annotations, etc. When using Set .

0
source

I am using spring 4.0.1, hibernate 4.3.5, jackson 1.9.2 and STS IDE

I had the same exception, but it was resolved by annotating the installer @Transient idont know why, but it works fine

These are my User.class object classes

  //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<Telephone> telephones = new HashSet<Telephone>(0); @OneToMany(fetch = FetchType.LAZY, mappedBy = "user") public Set<Telephone> getTelephones() { return this.telephones; } public void setTelephones(Set<Telephone> 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; } } 

regarding jackson registration for my application, I used xml config

  <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="web.jsonConverters.HibernateAwareObjectMapper" /> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> 

and mapping class

 public class HibernateAwareObjectMapper extends ObjectMapper { public HibernateAwareObjectMapper() { Hibernate4Module hm = new Hibernate4Module(); registerModule(hm); } } 
0
source

Source: https://habr.com/ru/post/946338/


All Articles