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)
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!
source share