users@jersey.java.net

[Jersey] Re: How to configure JSON POJO in Jersey 2.0 client?

From: agks mehx <agksmehx_at_gmail.com>
Date: Mon, 29 Apr 2013 10:06:38 -1000

Thank you Andrzej! That is very helpful compared to the hack I was using (I
was registering JacksonJaxbJsonProvider.class). Your method is way cleaner
and I am glad Jersey exposes the features that way rather than having to
know specific classes.

I modified your method slightly to say client.register(new
JacksonFeature()) after creating the client, just to avoid creating a
config object. Hopefully that is not much less elegant and if it is I will
be happy to switch to the better method.

Thanks again!


On Sun, Apr 28, 2013 at 9:04 PM, Andrzej Leśkiewicz <
andrzej.leskiewicz_at_gmail.com> wrote:

> Hello,
>
> You need to put JacksonFeature into Your ClientConfig, before creating new
> client.
>
>
> instead of
> Client client = ClientBuilder.newClient();
> call
> ClientConfig cc = new ClientConfig().register(new
> JacksonFeature());
> Client client = ClientBuilder.newClient(cc);
>
> You can see it in Jersey examples
> https://github.com/jersey/jersey/tree/master/examples/json-jackson/src.
> I've also added small sample with generics in this commit
> https://github.com/aleskiewicz/AsycJsonView/commit/358574765ca3f76e21344fbd7b84260ce6f215e9
> .
>
>
> 2013/4/29 agks mehx <agksmehx_at_gmail.com>
>
>> The the following Exception is thrown by running the code that follows
>> it. What do I need to do to get JSON work properly in the client?
>>
>> Exception in thread "main"
>> org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:
>> MessageBodyReader not found for media type=application/json, type=class
>> com.younum.test.foo.User, genericType=class com.younum.test.foo.User.
>> at
>> org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:173)
>>
>> -----------------
>>
>> package com.example.test.foo;
>>
>> import javax.ws.rs.client.Client;
>> import javax.ws.rs.client.ClientBuilder;
>> import javax.ws.rs.client.WebTarget;
>> import javax.ws.rs.core.MediaType;
>> import javax.xml.bind.annotation.XmlRootElement;
>>
>> @XmlRootElement
>> public class User {
>>
>> public String id;
>> public String name;
>>
>> public static void main (String [] args) {
>> Client client = ClientBuilder.newClient();
>> WebTarget web_target = client
>> .target("https://graph.facebook.com/skra95");
>> User me = web_target
>> .request()
>> .accept(MediaType.APPLICATION_JSON_TYPE)
>> .buildGet()
>> .property("access_token", "a_valid_access_token")
>> .invoke(User.class);
>> System.out.println(me);
>> }
>>
>> }
>>
>>
>
>
> --
> Best regards
> Andrzej Leśkiewicz
>