chris_sencha chris_sencha - 2 years ago 163
Java Question

json parsing with resttemplate

I have a json response as below

"@odata.context": "some context value here",
"value": [{
"": "odata id value1",
"@odata.etag": "W/\"CQEet/1EgOuA\"",
"Id": "id1",
"Subject": "subject1"
}, {
"": "odata id value2",
"@odata.etag": "W/\"CyEet/1EgOEk1t/\"",
"Id": "id2",
"Subject": "subject2"

How do I create a bean class(MyMessage) to parse the "value" using spring resttemplate?

RestTemplate rest = new RestTemplate();
ResponseEntity<MyMessage> response =, HttpMethod.GET, entity, MyMessage.class);

Could someone please help?

Answer Source

Annotate bean properties with @JsonProperty to set JSON field name for the property if it is different.


JsonProperty annotation and When is the @JsonProperty property used and what is it used for?

Example (bean properties are public for example simplicity):

MyMessage class:

public class MyMessage {

    public String context;

    public Value[] values;

Value class:

// PascalCaseStrategy is to resolve Id and Subject properties
public class Value {

    public String odataId;

    public String odataEtag;

    public String id;
    public String subject;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download