I am developing a typical Java EE 7 application on Glassfish 4.0, using Netbeans 7.4. I have a set of JPA entities with all kinds of relationships. As it is probably a well-known issue, I face a problem (most likely caused by a serialization loop) when I try to expose an entity with a bidirectional relationship with another one using a JAX-RS service.
I have searched a lot for the solution. Apparently, the most clean way of solving this is to use Jackson providers instead of Jersey built-in ones. However, it has not been a success for me. I may be doing something wrong in the middle, and any hint would be much appreciated.
First Entity:
@Entity
public class User implements Serializable {
@Id
private long id;
...
@OneToMany(
mappedBy = "owner",
cascade = CascadeType.ALL
)
private List<Playlist> playlists;
...
}
Second Entity:
@Entity
public class Playlist {
@Id
@GeneratedValue
private long id;
...
@NotNull
@ManyToOne
private User owner;
...
}
JAX-RS Service:
@Path("/user")
@Stateless
public class UserResource {
@Inject
private UserService userService;
@GET
@Path("/{username}")
@Produces(MediaType.APPLICATION_JSON)
public Response getUser(@PathParam("username") String username) {
return Response.ok(userService.getUser(username)).build();
}
}
I have downloaded and included latest Jackson binary (jackson-core-2.3.1.jar) in my classpath and I can actually see something related to jackson in Glassfish server log when I deploy my application. If the returned user entity does not have any associated playlists, everything is fine. However, with one or more playlists for the specified user, a HTTP 500 error is returned and nothing shows up in Glassfish server log.
Thanks in advance for any help.
UPDATE
After lots of back and forth. I am still unable to make jackson work. I moved to Wildfly (Jboss 8), and I cannot make it use the provided version of jackson instead of the built-in one. No matter which version of jackson is added in the classpath (with or without Maven), the error always indicates org.codehaus.jackson.*.