本文介绍了带有GenericType的getEntity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从REST服务获取实体.我的代码是这样的:

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

我要发送这样的对象:new GenericType<List<MyObject>>() {}

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

像这样的方法:

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

        ClientResponse response = RESTConnectionUtils.callMetaService(url);

            result = response.getEntity(responseObject);

        .....

但是它会在运行时被评估为List而不是GenericType,并且会引发异常.

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

推荐答案

如何调用callRestWithUrl方法?

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

也许此网页可以帮助: http://persistentdesigns.com/wp/2009/10/jersey-rest-客户/

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

这篇关于带有GenericType的getEntity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 10:34