RESTEasy client: reconstructing an object

2019-02-24 10:28发布

问题:

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

and called it with:

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));

But I'm getting:

ClientResponseFailure: Unable to find a MessageBodyReader of content-type application/json;charset="utf-8" and type interface java.util.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.

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.

回答1:

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.



标签: java resteasy