0

I've used restTemplate to get the details from a third party API.

Where, below code give me a response in string (using response.getBody())

ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);

Example of JSON

{"Entries":[{"EntryId":"1","Field1":"1","Field2":"2"},{"EntryId":"2","Field1":"3","Field2":"4"}]}

I've also created a class called Entries,

@JsonIgnoreProperties(ignoreUnknown=true)
public class Entries {

    @JsonProperty("EntryId")
    private String entryId;

    @JsonProperty("Field1")
    private String field1;

    @JsonProperty("Field2")
    private String field2;

//getter and setters

Is there any way to map JSON Array with Entires class using RestTemplate?

2
  • I think restTemplate.getForEntity(resourceUrl, Entity.class); Commented Jul 15, 2017 at 13:29
  • @fg78nc Actually I've to pass some header params. So, I've used restTemplate.exchange(). I've just found a solution and which is posted below. Commented Jul 15, 2017 at 13:32

1 Answer 1

1

There are two ways,

[1] Using ObjectMapper

ObjectMapper mapper = new ObjectMapper();
        Entries obj = mapper.readValue(rrateResponse.getBody(), Entries.class);

[2] Passing an Entries class

ResponseEntity<Entries> result = restTemplate.exchange(uri, HttpMethod.GET, entity, Entries.class);

The only missing thing was an Entries which is mentioned below.

@JsonIgnoreProperties(ignoreUnknown=true)
public class Entries {

    @JsonProperty("Entries")
    private List<Entry> Entries;

//getter and setter
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.