Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Jersey/JAX-RS client unable to handle generics?

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?

like image 861
IAmYourFaja Avatar asked Feb 12 '23 07:02

IAmYourFaja


1 Answers

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>>(){});
like image 178
Paul Samsotha Avatar answered Feb 13 '23 21:02

Paul Samsotha