users@jersey.java.net

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

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Thu, 24 Mar 2011 15:44:13 -0700

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 +-