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

折月煮酒 提交于 2019-12-10 18:16:19

问题


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?


回答1:


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>>(){});


来源:https://stackoverflow.com/questions/27599903/why-is-jersey-jax-rs-client-unable-to-handle-generics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!