users@jersey.java.net

[Jersey] JAXB annotations are ignored while serializing to JSON

From: naresh <baliga_naresh_at_yahoo.com>
Date: Wed, 25 Mar 2015 07:27:00 -0700 (MST)

Hello,

I'm using Jersey 2.17. I have JAXB annotations on my representations and I'm
facing an issue getting Jersey to honor those annotations while serializing
to JSON.

Here is my config:

         resourceConfig = new ResourceConfig();

         resourceConfig.register( RequestContextFilter.class );

         resourceConfig.register( JacksonObjectMapperContextResolver.class
);
         resourceConfig.register( JacksonFeature.class );

         resourceConfig.register( DeclarativeLinkingFeature.class );
         resourceConfig.register( EntityFilteringFeature.class );

         resourceConfig.register( LoggingFilter.class );

         resourceConfig.property( "contextConfig", new
ClassPathXmlApplicationContext( "test-applicationContext.xml" ) );
         resourceConfig.packages( "com.mywebapp.rest" );

         return DeploymentContext.builder( resourceConfig ).contextPath(
"/mywebapp" ).build();


As you can see, I'm using Jackson to serialize and de-serialize both in JSON
as well as XML. My ObjectMapper context resolver is as below:

@Component
@Provider
public class JacksonObjectMapperContextResolver
      implements ContextResolver<ObjectMapper>
{
   private ObjectMapper objectMapper;

   public JacksonObjectMapperContextResolver ()
   {
      objectMapper = new ObjectMapper()
            .configure( SerializationFeature.WRAP_ROOT_VALUE, true )
            .configure( SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false
)
            .configure(SerializationFeature.INDENT_OUTPUT, true)
            .configure( MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME,
true )
            .setSerializationInclusion( JsonInclude.Include.NON_NULL )
            .configure( DeserializationFeature.UNWRAP_ROOT_VALUE, true )
            .configure( DeserializationFeature.WRAP_EXCEPTIONS, false );

      final AnnotationIntrospector jaxbIntrospector = new
JaxbAnnotationIntrospector( objectMapper.getTypeFactory() );
      final AnnotationIntrospector jacksonIntrospector = new
JacksonAnnotationIntrospector();
      final AnnotationIntrospectorPair annotationIntrospectorPair = new
AnnotationIntrospectorPair( jaxbIntrospector, jacksonIntrospector );

      objectMapper.getSerializationConfig().with( annotationIntrospectorPair
);
      objectMapper.getDeserializationConfig().with(
annotationIntrospectorPair );

   }

   public ObjectMapper getContext ( final Class<?> pClass )
   {
      return objectMapper;
   }
}


I've put breakpoints to determine that the above is being invoked and my
ObjectMapper is indeed in play. For instance if, I toggle the INDENT_OUTPUT
property, the effects are observed in the output.

My representation uses @XmlRootElement, @XmlType(propOrder),
@XmlAccessorType ( XmlAccessType.FIELD ), @XmlElementWrapper & @XmlElement.

The JSON output ignores all of the above in that:
Root element name is set to the name of the class, the collection objects
are set to the variable name, the properties are not in the order specified.

In short the output is JSON without JAXB annotations.

I know this isn't an issue with Jackson as I ran a sample program that used
pure Jackson, and it worked fine - the JAXB annotations were honored.

Based on the examples at
https://github.com/jersey/jersey/blob/2.17/examples/json-jackson
<https://github.com/jersey/jersey/blob/2.17/examples/json-jackson> , I know
this concept works.

I'm obviously missing some configuration item but after hours I cannot
figure it out.

Any help or pointers will be appreciated.



--
View this message in context: http://jersey.576304.n2.nabble.com/JAXB-annotations-are-ignored-while-serializing-to-JSON-tp7583218.html
Sent from the Jersey mailing list archive at Nabble.com.