-
Notifications
You must be signed in to change notification settings - Fork 38.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reading typed lists with RestTemplate [SPR-8263] #12911
Comments
Craig Walls commented I forgot to mention that the other way to handle this is to either pass in a class that extends ArrayList<Foo>... List<Foo> foos = restTemplate.getForObject("some uri", MyFooList.class); // MyFooList extends ArrayList<Foo> ...or if this is JSON in the response, I can create a holder class whose constructor takes a List<Foo> and is annotated with These work, but are really workarounds for not being able to specify a collection's type when requesting it with RestTemplate. |
Mike Youngstrom commented Another workaround is to use an array: Foo[] foos = restTemplate.getForObject("some uri", new Foo[0].getClass()); |
Arjen Poutsma commented The problem I see is that Lists would only work for certain media types, such as JSON, and perhaps XML. But even with these two media types I can see a lot of problems, as they both represent a tree structure (and not a sequence). JSON (or Jackson, rather) has some nice workarounds for this as shown above, but JAXB (for instance) does not, AFAIK. We could create some arbitrary way to convert these tree structures into a list, but I think that will cause more bugs than it will solve. Resolving as won't fix. |
chris marx commented You can return a typed list by doing: ResponseEntity<? extends ArrayList<User> responseEntity = restTemplate.getForEntity(restEndPointUrl, (Class<? extends ArrayList<User>)ArrayList.class, userId); and for a generic entity: ResponseEntity<? extends ArrayList<HashMap<String,Object>>> responseEntity = restTemplate.getForEntity(restEndPointUrl, (Class<? extends ArrayList<HashMap<String,Object>>>)ArrayList.class, parameterId); |
Bogdan Calmac commented Arjen, you wouldn't need to convert a tree to a list. If the invoker were able to pass in the information that a We could have a separate set of methods However, with the existing API we can use |
Sébastien Deleuze commented If I am understanding correctly what is discussed here, you may consider using |
Craig Walls opened SPR-8263 and commented
With RestTemplate's methods, it is possible to request an object of a specific type...e.g.:
Foo foo = restTemplate.getForObject("some uri", Foo.class);
But if I want to retrieve a list of Foos, this won't work:
List<Foo> foos = restTemplate.getForObject("some uri", List.class);
Because there's no way to specify the type of the List to return, I get back a List of Map instead. It'd be nice if there was a way to specify the List's generic type so that I can get back a List<Foo> and not have to extract Foo objects from Map objects.
1 votes, 4 watchers
The text was updated successfully, but these errors were encountered: