RESTEasy客户端:重建对象 [英] RESTEasy client: reconstructing an object
问题描述
我正在使用RESTEasy来使用REST服务,我正在尝试使用Twitter的搜索API。
I'm playing with RESTEasy to consume REST services, and I'm trying it out with Twitter's search API.
所以我创建了这个界面:
So I create this interface:
public interface SimpleClient {
@GET
@Path("search.json")
@Produces("application/json")
ClientResponse<Set<String>> getSearchResults(
@QueryParam("q") String hashtag,
@QueryParam("result_type") String resultType
);
}
并将其称为:
SimpleClient client =
ProxyFactory.create(SimpleClient.class,"http://search.twitter.com/");
ClientResponse<Set<String>> response =
client.getSearchResults("#wowodc","recent");
System.out.println(response.getEntity(Set.class));
但我得到:
ClientResponseFailure:无法找到内容类型application / json的MessageBodyReader; charset =utf-8并输入interface java.util.Set
ClientResponseFailure: Unable to find a MessageBodyReader of content-type application/json;charset="utf-8" and type interface java.util.Set
我尝试使用POJO而不是java.util.Set,但我得到了同样的异常。唯一没有抛出异常的是使用String而不是Set。
I have tried using a POJO instead of java.util.Set, but I'm getting the same kind of exception. The only thing that didn't throw an exception is using String instead of Set.
通过阅读Web上的一些示例代码,我认为Set或POJO为实体类型可以工作,但它不适合我。对Twitter的查询确实返回了有效结果。
By reading some example code on the Web, I was thinking that Set or a POJO as the entity type would have work, but it doesn't for me. The query to Twitter did return valid results.
推荐答案
您需要确保包含可以解组JSON响应的RESTEasy提供程序。有一个基于你可以使用的Jackson解析器库,它在docs 这里。
You need to make sure you include a RESTEasy provider that can unmarshal JSON responses. There's a one based on the Jackson parser library that you can use, it's described in the docs here.
这篇关于RESTEasy客户端:重建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!