TransWikia.com

How can I retrieve many values of a same field in a GET request

Stack Overflow Asked by user2424634 on November 18, 2021

I have the json to retrieve below:

{
  "name": "João",
  "name": "Maria",
  "name": "José"
}

I made this way:

  ResponseEntity<List<Users>> responseEntityUsers = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Users.class);

But I got error.

My Users class is below:

public class Users {

     private String name;

     public String getName() {
        return name;
     }

     public void setName(String name) {
       this.name = name;
     }
}

2 Answers

You need to design your JSON for this class:

[
    {
    "name":"João"
    },{
    "name":"Maria"
    },{
    "name":"José"
    }
]

Answered by Mojtaba jalambadani on November 18, 2021

You need to design your JSON correctly, it is not well-formed JSON. It should be using an array of values for a specific attribute, like this:

{
  “names”: [“João”, “Maria”, "José"]
}

Notice that I have propositionally changed the attribute name to "names". Which is a good practice when designing your JSON to transport your data. That change also will impact your Model class, that instead of String must have a String array:

public class Users {

     private String[] names;

     public String[] getNames() {
        return names;
     }

     public void setNames(String[] names) {
       this.names = names;
     }
}

I wish you the best, cheers!

Answered by rod.dinis on November 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP