I have a Jersery/JAX-RS client that hits a RESTful API (JSON) that is supposed to return a list of my POJOs:
// Hits: GET localhost:8080/myapp/fizz/widget/{widget_id}
@Override
public List<Widget> getWidgetsByUser(Long id) {
return webResource.path("fizz").path("widget").path(id.toString()).get(List.class);
}
And a driver to test the client with:
public class Driver {
public static void main(String[] args) {
Driver d = new Driver();
d.run();
}
public void run() {
MyAppService myService = getSomehow();
List<Widget> widgets = myService.getWidgetResource().getWidgetsByUser(2L);
for(Widget widget : widgets) {
System.out.println("\t...and it found Widget #" + widget.getCaseId());
}
}
}
When I run this, I get:
Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.me.myapp.Widget
<stack trace omitted for brevity>
The exception is being thrown from the for-loop:
for(Widget widget : widgets) {
Which tells me the client is sort of working, but that I don't have it configured correctly.
So either Jersey is trying to return pure JSON and isn't attempting to map it back to a list of Widgets, or I am invoking the get(Class<?>)
method incorrectly. Any ideas?
The comment by Pavel Horal is correct. Without a known type, Jackson (the underlying deserilaizer) will map to LinkedHashMap
, so it will return List<LinkedHashMap>
Fix:
For generic types, we should use the other get()
method, which takes a GenericType
argument. So we should do something like
...get(new GenericType<List<Widget>>(){});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With