Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Now I wanted to Map this data to my class DTO but there I get an "error" because the DTO has no data field. I want it in a List or Array of my class. Like:

List<MyClass> list = restTemplate.getForObject(url, MyClass.class);

I hope you know what I mean?

i think your response json is wrong, because in data[0] keys are mulltiple, which is wrong. we know json should have unique key. value can be same. – Sangram Badi Oct 6, 2016 at 12:33 The JSON Object is valid, i just edited for example with "parma" in reality there are other objects – Markus G. Oct 6, 2016 at 12:37 i think you need to take Map<String,Object> and put your all keys in it, then put this map in to List<Object> – Sangram Badi Oct 6, 2016 at 12:42

One approach comes to mind is to convert the JSON response to a Map<String, List<MyClass>> and then query the map, i.e. map.get("data"), to get the actual List<MyClass>.

In order to convert the JSON response to Map<String, List<MyClass>>, you need to define a Type Reference:

ParameterizedTypeReference<Map<String, List<MyClass>>> typeRef = 
                           new ParameterizedTypeReference<Map<String, List<MyClass>>>() {};

Then pass that typeRef to the exchange method like the following:

ResponseEntity<Map<String, List<MyClass>>> response = 
                                 restTemplate.exchange(url, HttpMethod.GET, null, typeRef);

And finally:

System.out.println(response.getBody().get("data"));

If you're wondering why we need a type reference, consider reading Neal Gafter's post on Super Type Tokens.

Update: If you're going to deserialize the following schema:

"data": [], "paging": {}

It's better to create a dumb container class like the following:

class JsonHolder {
    private List<MyClass> data;
    private Object paging; // You can use custom type too.
    // Getters and setters

Then use it in your RestTemplate calls:

JsonHolder response = restTemplate.getForObject(url, JsonHolder.class);
System.out.println(response.getData()); // prints a List<MyClass>
System.out.println(response.getPaging()); // prints an Object
                I can think of any solution with just RestTemplate being involved. I guess you should use Jackson directly and manually deserialize the JSON string to List<MyClass>
– Ali Dehghani
                Oct 6, 2016 at 13:23
                One more question, when I have another structure after the "data" set how do I handle this?  like pastebin.com/mUUm6Tib
– Markus G.
                Oct 6, 2016 at 13:45
                +1 for using ParameterizedTypeReference - I started out creating a "MyClassList extends List<MyClass>" object and then doing MyClassList.class as the return type, but that is much cleaner
– Matt
                Dec 1, 2016 at 12:17

You can use Spring's ResponseEntity and array of MyClass. Below code will return an array of MyClass wrapped around

For e.g.

ResponseEntity<MyClass[]> response = restTemplate.exchange(
   HttpMethod.GET,
   request,
   MyClass[].class
// response null check etc. goes here
MyClass[] myArr = response.getBody();
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.