← back to Rants
RSS feed

Consuming @GenericType REST-webservices with jersey-client

I just lost 2 hours of my life trying to consume a RESTful webservice that returns a java.util.List

… but jersey always complained about a missing message body reader:

SEVERE: A message body reader for Java class java.util.List, 
and Java type java.util.List<MyClass>, 
and MIME media type application/json was not found 

Turns out you have to add a matching provider to your <pre>com.sun.jersey.api.client.Client</pre>:

ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getClasses().add(JacksonJsonProvider.class);
client = Client.create(clientConfig);
WebResource r = client.resource(endpoint);
List<MyClass> list = r.accept(MediaType.APPLICATION_JSON).
    get(new GenericType<List&ltMyClass>>() {
  });

Thanks to cobusbernard on stackoverflow for that one. I hope this’ll save somebody some time as it’s a bit hard to find.

by nsn on 2012-30-08 tags: programming jee java jersey jackson

comments powered by Disqus