I’ve written a client for a REST service using Jersey. For some reason, when the JSON to be unmarshalled has an empty array, the List object has a single element in it. That object has all members set to null as if it was just constructed with the default constructor.
Order.java:
@XmlRootElement
public class Order {
private int id;
private List<Photo> photos;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<Photo> getPhotos() {
return photos;
}
}
Client code that consumes the JSON service:
Client client = Client.create();
WebResource webResource = client.resource(environment.url);
Order order = webResource.path("Orders")
.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
.accept(MediaType.APPLICATION_JSON_TYPE)
.post(Order.class, formData);
The logs show that the JSON returned was:
{"id":704,"photos":[]}
However, the List “photos” is not empty as expected, but contains a single Photo object. All members of this object are null as if it was constructed with a default constructor.
What is creating this object and why?
More details: A similar problem exists if instead of a List of objects, I attempt to deserialize a List of enums. In this case I also get a List of size 1 this time just containing “null”.
I never found a decent answer and just reverted to using Gson for all the marshalling and unmarshalling of the JSON. It is actually cleaner now as GSON imposes fewer constraints on my POJOs (no need for annotations, or mutability).
This is not the solution I was originally looking for. I just posted it as the next best thing (which turned out to be better!).