JacksonJaxbJsonProvider default objectmapper mapping

大兔子大兔子 提交于 2019-12-13 02:54:46

问题


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 }

回答1:


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());


来源:https://stackoverflow.com/questions/36546087/jacksonjaxbjsonprovider-default-objectmapper-mapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!