0

Jersey is not showing a list in the JSON output when I retrieve the object using Hibernate. The list within the object is defined like this:

@OneToMany(cascade=CascadeType.ALL)
@OrderColumn
private List<Project> projects = new ArrayList<Project>();

When I retrieve the object (which also contains the projects list), I get the normal fields (ints and Strings and such), but not this list. When I use the debugger, I can see that the list is indeed there, but Jersey doesn't output it in JSON.

  • Does Project have the appropriate JSON or XML annotations (JAXB)? – John McClean Sep 20 '15 at 19:19
  • Well, it does have the `@Entity` annotation, as well as an `@Id` annotation. It does persist to the database, I can see it in the debugger once it's retrieved. –  Sep 20 '15 at 19:20
  • Those are JPA (Java Persistance API) annotations. Depending on how you have Jersey configured, you may need to add JAXB (Java Architecture for XML Binding) annotations also. They tell the serializer what form the JSON (or XML) output should take. Are you using Jackson to serialize to JSON? – John McClean Sep 20 '15 at 19:24
  • I'm not sure. I'm using Jersey, which maybe uses Jackson for mapping? –  Sep 20 '15 at 19:26
  • That would be pretty common, but it's configurable. Are you able to convert any responses to JSON? If yes - take a look at the objects it can serialize - if you have a custom Object that can be serialized, it probably has some serialization related annotations on it (you can copy those). If not, you may need to configure Jackson to do the serialization first. You can try adding these annotations to Project at the class level and see if it works for - it should if you hava Jackson & JAXB configured (@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "") @XmlRootElement(name = "project")) – John McClean Sep 20 '15 at 19:35
  • Well, I didn't run into any errors, but the list isn't showing up in the JSON output. –  Sep 20 '15 at 19:43
  • Do you mean like this: `@Entity @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name="project") public class Project {` –  Sep 20 '15 at 19:44
  • Yep. So you probably don't have a JSON Serializer configured that supports JAXB annotations. Here is an example of how to configure them - http://stackoverflow.com/questions/18872931/custom-objectmapper-with-jersey-2-2-and-jackson-2-1. (And FWIW if you happen to be using that same combo - we maintain a library that set all that up for you https://github.com/aol/micro-server/tree/master/micro-grizzly-with-jersey) – John McClean Sep 20 '15 at 19:53

1 Answers1

1

It looks like you need to configure a JSON Serializer such as Jackson. The answers to this question have some guidance on how to do that.

Once you have Jackson with JAXB support configured, you will need to add appropriate JAXB annotations to the Project class (either XML based one or JSON based ones, the serializer can be configured to support either or both). So, for example adding this to Project

 @XmlAccessorType(XmlAccessType.FIELD) 
 @XmlType(name = "") 
 @XmlRootElement(name = "project")) 
 public class Project {

Should be enough to serialize Project and it's fields to JSON.

Community
  • 1
  • 1
John McClean
  • 5,225
  • 1
  • 22
  • 30