users@jersey.java.net

[Jersey] Re: ClientResponse.getEntity has problem with @JsonTypeInfo annotation

From: Pengfei Di <pengfei.di_at_match2blue.com>
Date: Fri, 25 Mar 2011 08:42:54 +0100

Hello Jakub, hello Tatu,

thanks a lot for your replies! You are right, that I forgot to confiure
the client to use POJO mapping feature.
However, I get now some strange exceptions after I have done the
configuration, when the getEntity method is called:

Exception in thread "main" java.lang.VerifyError: class
org.codehaus.jackson.xc.JaxbAnnotationIntrospector overrides final
method
findDeserializer.(Lorg/codehaus/jackson/map/introspect/Annotated;)Ljava/lang/Object;
     at java.lang.ClassLoader.defineClass1(Native Method)
     at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
     at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
     at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
     at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
     at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
     at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
     at java.security.AccessController.doPrivileged(Native Method)
     at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
     at
org.codehaus.jackson.jaxrs.MapperConfigurator._resolveIntrospector(MapperConfigurator.java:169)
     at
org.codehaus.jackson.jaxrs.MapperConfigurator._resolveIntrospectors(MapperConfigurator.java:144)
     at
org.codehaus.jackson.jaxrs.MapperConfigurator._setAnnotations(MapperConfigurator.java:131)
     at
org.codehaus.jackson.jaxrs.MapperConfigurator.getDefaultMapper(MapperConfigurator.java:70)
     at
org.codehaus.jackson.jaxrs.JacksonJsonProvider.locateMapper(JacksonJsonProvider.java:575)
     at
org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:392)
     at
com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.readFrom(JacksonProviderProxy.java:96)
     at
com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:552)
     at
com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:505)



Pengfei


On 03/24/2011 11:44 PM, Tatu Saloranta wrote:
> On Thu, Mar 24, 2011 at 2:14 PM, Jakub Podlesak
> <jakub.podlesak_at_oracle.com> wrote:
>> Hello Pengfei,
>>
>> On 03/24/2011 10:29 AM, Pengfei Di wrote:
>>> Hello,
>>>
>>> I have some problem when I use ClientResponse.getEntity to parse a jaxb
>>> class from a JSON representation, if @JsonTypeInfo annotation is applied.
>>> Here is the code with problem:
>>>
>>> @JsonTypeInfo(use = Id.NAME, include = As.WRAPPER_OBJECT)
>>> @XmlRootElement
>>> public class MyTest
>>> {
>>> private int code;
>>> private String msg;
>>> //setter and getter
>>> }
>>>
>>> The Json string looks like:
>>> {"MyTest":{"code":207,"msg":"blah blah"}}
>>> and the parsed result of MyTest contains either 0 or null.
>>>
>> it seems, you do not have your client configured to use the JSON POJO
>> mapping feature (see [1]).
>> I have added this snippet to the Jersey user guide doc only recently:
>>
>> ClientConfig clientConfig = new DefaultClientConfig();
>> clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
>> Boolean.TRUE);
>> Client client = Client.create(clientConfig);
>>
>>
>>> However, if I remove the include part of the @JsonTypeInfo, there is no
>>> problem any more, and the Json string looks like:
>>> {"@type":"MyTest","code":207,"msg":"blah blah"}
>>>
>> That is strange, from top of my head, this does not seem to come from Jackson,
>> but i might be mistaken.
> This could be produced by Jackson when applying different
> @JsonTypeInfo settings; specifically,
> @JsonTypeInfo(use = Id.NAME, include = As.PROPERTY)
> // (or Id.SIMPLE_CLASS)
>
> Either way, Jakub is right in that POJO setting must be enabled in
> both cases; otherwise type information is not going to be properly
> used. In first case problem is not JSON being produced, but rather
> that reader is not aware that it is to expect type information in form
> of wrapper JSON Object.
> In second case there is still problem that "@type" is ignored;
> however, depending on settings, this may not throw an error. So
> problem is just hidden, ignored, but there is a problem nonetheless.
>
> -+ Tatu +-


-- 
Pengfei Di
Software Developer Backend
match2blue software development GmbH
Leutragraben 1
07743 Jena
Tel.: +49 3641 573 3474
Fax: +49 3641 573 3479
Email: pengfei.di_at_match2blue.com
Web: http://www.match2blue.com
Blog: http://blog.match2blue.com
Registergericht: Amtsgericht Jena
Registernummer: HRB 503726
Geschäftsführer: Stephanie Renda