简体   繁体   中英

Spring Rest Response into Array

So I am developing a REST application and I have the service done to return the json/xml. I have a list and I want to read it into an array. It's returned from a GET request like

http://localhost:8080/todo

I could iterate through the

http://localhost:8080/todo/get/1

and just do that through the size, but I wanted to see if I could get the whole response into an array. Its a json response that looks like:

[{"id":1,"task":"Assemble Team"},{"id":2,"task":"Determine Roles"},{"id":3,"task":"Split Up Work"},{"id":4,"task":"Do Individual Tasks"},{"id":5,"task":"doit"}]

Right now I'm using Spring Boot and it looks like

int i = restTemplate.getForObject("http://localhost:8080/todo/size", int.class);
        System.out.println("Size: " + i);
        Task quote = restTemplate.getForObject("http://localhost:8080/gs-rest-service-0.1.0/todo/get/3", Task.class);

that's to get one of the objects. but is there a way to read the entire json list into an array or something instead of iterating through, calling GET requests for each item on the list?

Should I create another class that holds an arraylist of the Task class (which just has an id and task string, and standard getters/setters)? I'm not sure how that would work.

I tried having a class that would hold a bunch of the 'quotes':

 @JsonIgnoreProperties(ignoreUnknown = true)
public class Value {

private List<Quote> Q = new ArrayList<Quote>();

public Value() {

}

public List<Quote> getQuote() {
    return this.Q;
}


public void setList(List<Quote> quote) {
    this.Q = (ArrayList<Quote>) quote;
}

@Override
public String toString() {
    return Q.toString();
}

and in the main part:

Value V = restTemplate.getForObject("http://localhost:8080/todo", Value.class);

But I just get

 Can not deserialize instance of hello.Value out of START_ARRAY token

Thanks.

Make use of exchange methods available in Rest Template.

ResponseEntity<List<Quote>> res = template.exchange(
rootUrl,
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<Quote>>() {});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM