Why does a Jersey client insert an object into my list when it should be empty?

I wrote a client for a REST service using Jersey. For some reason, when JSON for unmarshalled has an empty array, the List object has one element in it. This object has all members equal to null, as if it were only constructed using 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 uses 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);

Logs show that the returned JSON was:

{"id":704,"photos":[]}

However, the “photos” list is not empty as expected, but contains only one “Photo” object. All members of this object have a null value, as if it were constructed using the default constructor.

What creates this object and why?


. , . 1 , "null".

+3
2

Gson unmarshalling JSON. , , GSON POJO ( ).

ClientResponse response = webResource.path("Orders")
        .type(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
        .accept(MediaType.APPLICATION_JSON_TYPE)
        .post(ClientResponse.class, formData);

Order order = new Gson().fromJson(response.getEntity(String.class), Order.class);

, . ( !).

0

Order:

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

Jackson serializer, .

UPD. , Photo.

+1

All Articles