JacksonJaxbJsonProvider默认对象映射器 [英] JacksonJaxbJsonProvider default objectmapper mapping

查看:544
本文介绍了JacksonJaxbJsonProvider默认对象映射器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下

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).

我想将com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider用于其他数据模型的JSON序列化/反序列化.

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

JacksonJaxbJsonProvider的默认实现非常适合将超类作为抽象类的我的JAXB模型.但是一旦提供了自己的自定义ObjectMapper(如下所示),就不会使用JacksonJaxbJsonProvider的默认实现.也就是说,自定义ObjectMapper无法正确转换为我的抽象类声明的JAXB批注和字段,因为它找不到在抽象类中声明的字段.

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.

因此,我想根据相关的JAXB模型同时使用自定义ObjectMapper和JacksonJaxbJsonProvider的默认实现.

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 }

推荐答案

此呼叫

.setAnnotationIntrospector(createJaxbJacksonAnnotationIntrospector());

是为combinedObjectMapper添加JAXB支持的内容.因此,如果您希望的JAXB支持,只需添加相同的调用即可.

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

这篇关于JacksonJaxbJsonProvider默认对象映射器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆