getEntity with GenericType

試著忘記壹切 提交于 2019-12-13 04:48:41

问题


Trying to get an Entity from a REST service. My code is like this:

I want to send in an object like this: new GenericType<List<MyObject>>() {}

to a methode like this:

public static Object callRestWithUrl(String url, Class<?> responseObject)
            throws MetaServiceException {

        ClientResponse response = RESTConnectionUtils.callMetaService(url);

            result = response.getEntity(responseObject);

        .....

But it gets evaluated to List during runtime and not GenericType and an exception is thrown.


回答1:


How do you invoke your callRestWithUrl method?

It could be that it would work better if your method had the signature:

public static Object callRestWithUrl(String url, GenericType<List<?>> responseObject)

And that you then invoked it as this:

List<...> list = callRestWithUrl(new GenericType<List<MyObject>>() {});

Maybe this web page can help: http://persistentdesigns.com/wp/2009/10/jersey-rest-client/




回答2:


For some reason it started working as is. No idea why. The input to the method needed to be generic.



来源:https://stackoverflow.com/questions/10587378/getentity-with-generictype

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