users@jersey.java.net

[Jersey] Re: how to rename XmlRootElement in JSON

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Fri, 27 May 2011 15:58:08 +0200

Hi Pengfei,

I think it is a config issue on your side.
Have just quickly tried to tweak our jacksonjsonprovider example [1].
I changed the MyObjectMapperProvider.getContext method to:

--8<--
     @Override
     public ObjectMapper getContext(Class<?> type) {

         ObjectMapper result = new ObjectMapper();
         result.configure(Feature.WRAP_ROOT_VALUE, true);

         AnnotationIntrospector jaxbIntrospector = new
JaxbAnnotationIntrospector();
         AnnotationIntrospector jacksonIntrospector = new
JacksonAnnotationIntrospector();
         final Pair combinedIntrospector = new
AnnotationIntrospector.Pair(jaxbIntrospector, jacksonIntrospector);
         
result.getDeserializationConfig().setAnnotationIntrospector(combinedIntrospector);
         
result.getSerializationConfig().setAnnotationIntrospector(combinedIntrospector);

         return result;
     }
-->8--

as suggested by [2], and the JAXB annotation value gets reflected by the
output JSON data.
No need for any redundant annotations.

HTH,

~Jakub

[1]http://download.java.net/maven/2/com/sun/jersey/samples/jacksonjsonprovider/1.8-SNAPSHOT/jacksonjsonprovider-1.8-SNAPSHOT-project.zip
[2]http://wiki.fasterxml.com/JacksonJAXBAnnotations

On 03/15/2011 08:48 PM, Pengfei Di wrote:
> Hi Tatu,
>
> thanks a lot for the reply. I found an article [1] about the jackson
> annotations, which says that it is possible to configure jackson to
> use name property of @XmlRootElement.
>
> "@link javax.xml.bind.annotation.XmlRootElement Jackson 1.7
> <http://wiki.fasterxml.com/JacksonRelease17> allows this to be
> recognized when root-value wrapping is enabled, and JAXB annotation
> introspector is used."
>
> Now I do have the class name included in the json representation,
> however, it seems the name specified after @xmlRootElement is not
> recognized by jackson.
> What I have done now is using 2 annotations, which of course is not a
> good solution.
>
> @@JsonTypeName( value = "account" )
> @XmlRootElement( name = "account" )
> public class MyAccount
> {
> int x;
> }
>
> Best Regards
> Pengfei
>
> [1] http://wiki.fasterxml.com/JacksonJAXBAnnotations
>
>
>
> On 03/15/2011 06:25 PM, Tatu Saloranta wrote:
>> On Tue, Mar 15, 2011 at 8:54 AM, Pengfei Di<pengfei.di_at_match2blue.com> wrote:
>>> Hello,
>>>
>>> I tried to rename the root element of a jaxb class, that is easy for XML
>>> representation. However, it seems impossible for JSON representation.
>>> 1. put a JAXBContextResolver, as written in the Example5.5 of jersey guide.
>>> 2. add jackson 1.7.0 into the pom.xml. (jackson-core-lgpl 1.7.0 and
>>> jackson-mapper-lgpl 1.7.0)
>>> Did I miss something?
>>>
>>> The JAXB class looks like:
>>> @XmlRootElement( name = "account" )
>>> public class MyAccount
>>> {
>>> int x;
>>> }
>> When using Jackson, there will not be a root element (JSON structure
>> differs from XML), so you might not be using POJO mapping with Jersey?
>> Other json producing methods do add wrapper property.
>>
>> -+ 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
>
>