我正在使用 RESTEasy 来使用 REST 服务,我正在尝试使用 Twitter 的搜索 API。
所以我创建了这个界面:
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: 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。
通过阅读 Web 上的一些示例代码,我认为 Set 或 POJO 作为实体类型会起作用,但它不适合我。对 Twitter 的查询确实返回了有效结果。
最佳答案
您需要确保包含一个可以解码 JSON 响应的 RESTEasy 提供程序。有一个基于 Jackson 解析器库的,您可以使用,它在文档中描述 here .
关于java - RESTEasy 客户端 : reconstructing an object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6004660/