users@jersey.java.net

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

From: Andrzej Le¶kiewicz <andrzej.leskiewicz_at_gmail.com>
Date: Tue, 30 Apr 2013 08:57:26 +0200

@agks mehx
I think JacksonFeature is actually
registering org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider.

As for ClientConfig vs Client - You can store the config somewhere and
reuse it later, it depends on how often do You create the client or hoe
often Your configuration changes etc., so as long as it works for You -
then I guess it's fine :)

@Tony
It's located in javax.annotation:javax.annotation-api

below is a short fragment of my project dependencies which specify this JAR

+--- org.glassfish.jersey.core:jersey-common:2.0+ -> 2.0-rc1
| +--- javax.ws.rs:javax.ws.rs-api:2.0-rc3
| | \--- javax.annotation:javax.annotation-api:1.2-b02

It is indirectly required by Jersey, So I guess, You might be using wrong
Jersey version ?

Annotation itself is specified in JSR 250: Common Annotations for the
JavaTM Platform, Maintenance Release 2 (
http://jcp.org/aboutJava/communityprocess/maintenance/jsr250/jsr250changelog_2.html)
which is still a draft



2013/4/30 Tony Anecito <adanecito_at_yahoo.com>

> Thanks this helped me also. I am getting an exception about
> javax/annotation/Priority not being found when register is called.
>
> Anyone know where I can find a jar that has it? I have been googling and
> no luck so far.
>
> Thanks,
> -Tony
>
> ------------------------------
> *From:* agks mehx <agksmehx_at_gmail.com>
> *To:* users <users_at_jersey.java.net>
> *Sent:* Monday, April 29, 2013 2:06 PM
> *Subject:* [Jersey] Re: How to configure JSON POJO in Jersey 2.0 client?
>
> 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
>
>
>
>
>


-- 
Best regards
Andrzej Le¶kiewicz