users@jersey.java.net

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

From: agks mehx <agksmehx_at_gmail.com>
Date: Tue, 30 Apr 2013 01:45:03 -1000

PS: I need to do this on the client side rather than server.


On Tue, Apr 30, 2013 at 1:38 AM, agks mehx <agksmehx_at_gmail.com> wrote:

> Perhaps I should ask this in the same thread. Is there a clean way to
> configure the JSON ObjectMapper so that I don't need to annotate all my
> classes with @JsonIgnoreProperties(ignoreUnknown = true) ?
>
> I found the following piece of code but I am not sure if it is the best
> way to do things, or whether it is applicable to Jersey 2.0 (from
> http://wiki.fasterxml.com/JacksonFAQJaxRs):
>
> // Customized {_at_code ContextResolver} implementation to pass ObjectMapper to use
> @Provider
> @Produces(MediaType.APPLICATION_JSON)
> public class JacksonContextResolver implements ContextResolver<ObjectMapper> {
> private ObjectMapper objectMapper;
>
> public JacksonContextResolver() throws Exception {
> this.objectMapper = new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
> }
> public ObjectMapper getContext(Class<?> objectType) {
> return objectMapper;
> }
> }
>
>
>
>
> On Mon, Apr 29, 2013 at 11:13 PM, Marek Potociar <
> marek.potociar_at_oracle.com> wrote:
>
>> All the external JAX-RS RI dependencies (read also as core Jersey
>> dependencies) can be found in the latest JAX-RS RI binary zip bundle:
>>
>>
>> https://maven.java.net/content/repositories/snapshots/org/glassfish/jersey/bundles/jaxrs-ri/2.0-SNAPSHOT/
>>
>> Look for a bundle with a name in format
>> "jaxrs-ri-2.0-<snapshot_timestamp>.zip. All the external dependencies are
>> in the "ext" directory.
>>
>> Marek
>>
>> On Apr 30, 2013, at 8:57 AM, Andrzej Le¶kiewicz <
>> andrzej.leskiewicz_at_gmail.com> wrote:
>>
>> @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
>>
>>
>>
>