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.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
JimmySmithJR
  • 301
  • 1
  • 9
  • 17
  • How about `List quotes = (List) restTemplate.getForObject(".../todo", java.util.List.class);` – xiaofeng.li Nov 21 '16 at 00:28
  • 1
    I actually found: http://stackoverflow.com/questions/22358872/how-to-convert-linkedhashmap-to-custom-java-object and used and yours together, thanks – JimmySmithJR Nov 21 '16 at 00:58

1 Answers1

2

Make use of exchange methods available in Rest Template.

ResponseEntity<List<Quote>> res = template.exchange(
rootUrl,
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<Quote>>() {});
s7vr
  • 73,656
  • 11
  • 106
  • 127
  • I think you need to declare `res` as `ResponseEntity>` – xiaofeng.li Nov 21 '16 at 00:44
  • This answers the question. I couldn't come up with a simpler approach. I wonder why there isn't an overload of `getForObject`. – xiaofeng.li Nov 21 '16 at 00:56
  • well instead of having an overload for each of http verbs spring exposes an exchange api – s7vr Nov 21 '16 at 00:58
  • Sorry, but this is a lot of code to do a simple get with parameterized return type. – xiaofeng.li Nov 21 '16 at 01:01
  • 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. – JimmySmithJR Nov 21 '16 at 01:09