ulab
ulab

Reputation: 1089

JacksonJaxbJsonProvider default objectmapper mapping

I'm using below code from this link to add custom de-serializer for one of my data model class (JSON to JAXB models conversion).

I would like to use com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider for my JSON serialization/de-serialization for other data models.

The default implementation of JacksonJaxbJsonProvider works perfectly for my JAXB models with super class as abstract class. But once I have provided my own custom ObjectMapper (as shown below), the default implementation of JacksonJaxbJsonProvider is not used. i.e the JAXB annotations and fields declared for my abstract class are not converted properly by Custom ObjectMapper as it cannot find the fields declared in abstract class.

So I would like to use the custom ObjectMapper and the default implementation of JacksonJaxbJsonProvider at the same time depending on the JAXB model in question.

40  package org.glassfish.jersey.examples.jackson;
41  
42  import javax.ws.rs.ext.ContextResolver;
43  import javax.ws.rs.ext.Provider;
44  
45  import com.fasterxml.jackson.databind.AnnotationIntrospector;
46  import com.fasterxml.jackson.databind.DeserializationFeature;
47  import com.fasterxml.jackson.databind.ObjectMapper;
48  import com.fasterxml.jackson.databind.SerializationFeature;
49  import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
50  import com.fasterxml.jackson.databind.type.TypeFactory;
51  import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
52  
53  /**
54   * TODO javadoc.
55   *
56   * @author Jakub Podlesak (jakub.podlesak at oracle.com)
57   */
58  @Provider
59  public class MyObjectMapperProvider implements ContextResolver<ObjectMapper> {
60  
61      final ObjectMapper defaultObjectMapper;
62      final ObjectMapper combinedObjectMapper;
63  
64      public MyObjectMapperProvider() {
65          defaultObjectMapper = createDefaultMapper();
66          combinedObjectMapper = createCombinedObjectMapper();
67      }
68  
69      @Override
70      public ObjectMapper getContext(final Class<?> type) {
71  
72          if (type == CombinedAnnotationBean.class) {
73              return combinedObjectMapper;
74          } else {
75              return defaultObjectMapper;
76          }
77      }
78  
79      private static ObjectMapper createCombinedObjectMapper() {
80          return new ObjectMapper()
81                  .configure(SerializationFeature.WRAP_ROOT_VALUE, true)
82                  .configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true)
83                  .setAnnotationIntrospector(createJaxbJacksonAnnotationIntrospector());
84      }
85  
86      private static ObjectMapper createDefaultMapper() {
87          final ObjectMapper result = new ObjectMapper();
88          result.enable(SerializationFeature.INDENT_OUTPUT);
89  
90          return result;
91      }
92  
93      private static AnnotationIntrospector createJaxbJacksonAnnotationIntrospector() {
94  
95          final AnnotationIntrospector jaxbIntrospector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
96          final AnnotationIntrospector jacksonIntrospector = new JacksonAnnotationIntrospector();
97  
98          return AnnotationIntrospector.pair(jacksonIntrospector, jaxbIntrospector);
99      }
100 }

Upvotes: 1

Views: 2114

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209004

This call

.setAnnotationIntrospector(createJaxbJacksonAnnotationIntrospector());

is what adds the JAXB support for the combinedObjectMapper. So if you want the JAXB support for the defaultObjectMapper, just add the same call.

final ObjectMapper result = new ObjectMapper();
result.setAnnotationIntrospector(createJaxbJacksonAnnotationIntrospector());

Upvotes: 2

Related Questions