1

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.

2
  • How about List<Quote> quotes = (List<Quote>) restTemplate.getForObject(".../todo", java.util.List.class); Commented Nov 21, 2016 at 0:28
  • 1
    I actually found: stackoverflow.com/questions/22358872/… and used and yours together, thanks Commented Nov 21, 2016 at 0:58

1 Answer 1

2

Make use of exchange methods available in Rest Template.

ResponseEntity<List<Quote>> res = template.exchange(
rootUrl,
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<Quote>>() {});
Sign up to request clarification or add additional context in comments.

5 Comments

I think you need to declare res as ResponseEntity<List<Quote>>
This answers the question. I couldn't come up with a simpler approach. I wonder why there isn't an overload of getForObject.
well instead of having an overload for each of http verbs spring exposes an exchange api
Sorry, but this is a lot of code to do a simple get with parameterized return type.
yep this worked perfectly, thanks. I'll have to read up more on exchange methods, I just got into using REST with spring and there seems to be so many things to know. Just figuring out which way to use REST (jersey, boot, resteasy, etc) took me awhile.

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.