1

I have a json response as below

{
    "@odata.context": "some context value here",
    "value": [{
        "@odata.id": "odata id value1",
        "@odata.etag": "W/\"CQEet/1EgOuA\"",
        "Id": "id1",
        "Subject": "subject1"
    }, {
        "@odata.id": "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 = rest.exchange(url, HttpMethod.GET, entity, MyMessage.class);

Could someone please help?

Michal Foksa
  • 11,225
  • 9
  • 50
  • 68
seba_sencha
  • 205
  • 4
  • 17
  • Are you sure you need to handle this property yourself ? I think you don't need to map it, the OData server part will find it itself and do what needs to be done. – Alexandre Fillatre May 08 '16 at 08:23
  • I am trying to use outlook mail api. They dont have a java client. https://msdn.microsoft.com/office/office365/api/mail-rest-operations#Getmessages . On client, we need to parse this message to get mail details – seba_sencha May 08 '16 at 08:28

1 Answers1

2

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

See:

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 {

    @JsonProperty("@odata.context")
    public String context;

    @JsonProperty("value")
    public Value[] values;
}

Value class:

// PascalCaseStrategy is to resolve Id and Subject properties
@JsonNaming(PascalCaseStrategy.class)
public class Value {

    @JsonProperty("@odata.id")
    public String odataId;

    @JsonProperty("@odata.etag")
    public String odataEtag;

    public String id;
    public String subject;
}
Community
  • 1
  • 1
Michal Foksa
  • 11,225
  • 9
  • 50
  • 68