• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

dubboJackson序列化统一配置

武飞扬头像
tongxintong
帮助1

  1.  
    import com.fasterxml.jackson.annotation.JsonInclude;
  2.  
    import com.fasterxml.jackson.core.JsonEncoding;
  3.  
    import com.fasterxml.jackson.core.JsonGenerator;
  4.  
    import com.fasterxml.jackson.core.JsonParser;
  5.  
    import com.fasterxml.jackson.databind.*;
  6.  
    import com.fasterxml.jackson.jaxrs.cfg.AnnotationBundleKey;
  7.  
    import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
  8.  
    import com.fasterxml.jackson.jaxrs.json.JsonEndpointConfig;
  9.  
    import com.fasterxml.jackson.jaxrs.util.ClassKey;
  10.  
    import org.jboss.resteasy.annotations.providers.NoJackson;
  11.  
    import org.jboss.resteasy.annotations.providers.jackson.Formatted;
  12.  
    import org.jboss.resteasy.util.DelegatingOutputStream;
  13.  
    import org.jboss.resteasy.util.FindAnnotation;
  14.  
     
  15.  
    import javax.ws.rs.Consumes;
  16.  
    import javax.ws.rs.Produces;
  17.  
    import javax.ws.rs.core.MediaType;
  18.  
    import javax.ws.rs.core.MultivaluedMap;
  19.  
    import javax.ws.rs.ext.Provider;
  20.  
    import java.io.IOException;
  21.  
    import java.io.InputStream;
  22.  
    import java.io.OutputStream;
  23.  
    import java.lang.annotation.Annotation;
  24.  
    import java.lang.reflect.Type;
  25.  
    import java.util.TimeZone;
  26.  
    import java.util.concurrent.ConcurrentHashMap;
  27.  
     
  28.  
    /**
  29.  
    * Custom Jackson Provider which ignoring unknown properties,doing not write null value by default.
  30.  
    *
  31.  
    * @author 01375295
  32.  
    */
  33.  
    @Provider
  34.  
    @Consumes({"application/* json", "text/json"})
  35.  
    @Produces({"application/* json", "text/json"})
  36.  
    public class CotpResteasyJackson2Provider extends JacksonJaxbJsonProvider {
  37.  
    public CotpResteasyJackson2Provider() {
  38.  
    super(getObjectMapper(), DEFAULT_ANNOTATIONS);
  39.  
    }
  40.  
     
  41.  
    private static synchronized ObjectMapper getObjectMapper() {
  42.  
    ObjectMapper objectMapper = new ObjectMapper();
  43.  
    objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
  44.  
    /*
  45.  
    该特性决定是否在writeValue()方法之后就调用JsonGenerator.flush()方法。
  46.  
    当我们需要先压缩,然后再flush,则可能需要false。
  47.  
    objectMapper.disable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
  48.  
    */
  49.  
     
  50.  
    //setting ignore unknown properties.
  51.  
    objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
  52.  
    //setting do not write null value of object.
  53.  
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
  54.  
    objectMapper.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
  55.  
    objectMapper.setTimeZone(TimeZone.getDefault());
  56.  
    return objectMapper;
  57.  
    }
  58.  
     
  59.  
     
  60.  
    @Override
  61.  
    public boolean isReadable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
  62.  
    if (FindAnnotation.findAnnotation(aClass, annotations, NoJackson.class) != null) {
  63.  
    return false;
  64.  
    }
  65.  
    return super.isReadable(aClass, type, annotations, mediaType);
  66.  
    }
  67.  
     
  68.  
    @Override
  69.  
    public boolean isWriteable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
  70.  
    if (FindAnnotation.findAnnotation(aClass, annotations, NoJackson.class) != null) {
  71.  
    return false;
  72.  
    }
  73.  
    return super.isWriteable(aClass, type, annotations, mediaType);
  74.  
    }
  75.  
     
  76.  
    // Currently we need to override readFrom and writeTo because Jackson 2.2.1 does not cache correctly
  77.  
    // It does not allow to have a ContextResolver that chooses different mappers per Java type.
  78.  
     
  79.  
    private static class ClassAnnotationKey {
  80.  
    private AnnotationBundleKey annotations;
  81.  
    private ClassKey classKey;
  82.  
    private int hash;
  83.  
     
  84.  
    private ClassAnnotationKey(Class<?> clazz, Annotation[] annotations) {
  85.  
    this.annotations = new AnnotationBundleKey(annotations, AnnotationBundleKey.class);
  86.  
    this.classKey = new ClassKey(clazz);
  87.  
    this.hash = this.annotations.hashCode();
  88.  
    this.hash = 31 * this.hash this.classKey.hashCode();
  89.  
    }
  90.  
     
  91.  
    @Override
  92.  
    public boolean equals(Object o) {
  93.  
    if (this == o) {
  94.  
    return true;
  95.  
    } else if (o != null && this.getClass() == o.getClass()) {
  96.  
    ClassAnnotationKey that = (ClassAnnotationKey) o;
  97.  
    if (!this.annotations.equals(that.annotations)) {
  98.  
    return false;
  99.  
    } else {
  100.  
    return this.classKey.equals(that.classKey);
  101.  
    }
  102.  
    } else {
  103.  
    return false;
  104.  
    }
  105.  
    }
  106.  
     
  107.  
    @Override
  108.  
    public int hashCode() {
  109.  
    return this.hash;
  110.  
    }
  111.  
    }
  112.  
     
  113.  
    protected final ConcurrentHashMap<ClassAnnotationKey, JsonEndpointConfig> _readers
  114.  
    = new ConcurrentHashMap<ClassAnnotationKey, JsonEndpointConfig>();
  115.  
     
  116.  
    @Override
  117.  
    public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
  118.  
    throws IOException {
  119.  
    ClassAnnotationKey key = new ClassAnnotationKey(type, annotations);
  120.  
    JsonEndpointConfig endpoint;
  121.  
    endpoint = _readers.get(key);
  122.  
    // not yet resolved (or not cached any more)? Resolve!
  123.  
    if (endpoint == null) {
  124.  
    ObjectMapper mapper = locateMapper(type, mediaType);
  125.  
    endpoint = _configForReading(mapper, annotations, type);
  126.  
    _readers.put(key, endpoint);
  127.  
    }
  128.  
    ObjectReader reader = endpoint.getReader();
  129.  
    JsonParser jp = _createParser(reader, entityStream);
  130.  
    // If null is returned, considered to be empty stream
  131.  
    if (jp == null || jp.nextToken() == null) {
  132.  
    return null;
  133.  
    }
  134.  
    // [Issue#1]: allow 'binding' to JsonParser
  135.  
    if (((Class<?>) type) == JsonParser.class) {
  136.  
    return jp;
  137.  
    }
  138.  
    return reader.withType(genericType).readValue(jp);
  139.  
    }
  140.  
     
  141.  
    protected final ConcurrentHashMap<ClassAnnotationKey, JsonEndpointConfig> _writers
  142.  
    = new ConcurrentHashMap<ClassAnnotationKey, JsonEndpointConfig>();
  143.  
     
  144.  
    @Override
  145.  
    public void writeTo(Object value, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
  146.  
    MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
  147.  
    throws IOException {
  148.  
    entityStream = new DelegatingOutputStream(entityStream) {
  149.  
    @Override
  150.  
    public void flush() throws IOException {
  151.  
    // don't flush as this is a performance hit on Undertow.
  152.  
    // and causes chunked encoding to happen.
  153.  
    }
  154.  
    };
  155.  
    ClassAnnotationKey key = new ClassAnnotationKey(type, annotations);
  156.  
    JsonEndpointConfig endpoint;
  157.  
    endpoint = _writers.get(key);
  158.  
     
  159.  
    // not yet resolved (or not cached any more)? Resolve!
  160.  
    if (endpoint == null) {
  161.  
    ObjectMapper mapper = locateMapper(type, mediaType);
  162.  
    endpoint = _configForWriting(mapper, annotations, type);
  163.  
     
  164.  
    // and cache for future reuse
  165.  
    _writers.put(key, endpoint);
  166.  
    }
  167.  
     
  168.  
    ObjectWriter writer = endpoint.getWriter();
  169.  
    boolean withIndentOutput = false; // no way to replace _serializationConfig
  170.  
     
  171.  
    // we can't cache this.
  172.  
    if (annotations != null) {
  173.  
    for (Annotation annotation : annotations) {
  174.  
    if (annotation.annotationType().equals(Formatted.class)) {
  175.  
    withIndentOutput = true;
  176.  
    break;
  177.  
    }
  178.  
    }
  179.  
    }
  180.  
     
  181.  
    /* 27-Feb-2009, tatu: Where can we find desired encoding? Within
  182.  
    * HTTP headers?
  183.  
    */
  184.  
    JsonEncoding enc = findEncoding(mediaType, httpHeaders);
  185.  
    JsonGenerator jg = writer.getFactory().createGenerator(entityStream, enc);
  186.  
     
  187.  
    try {
  188.  
    // Want indentation?
  189.  
    if (writer.isEnabled(SerializationFeature.INDENT_OUTPUT) || withIndentOutput) {
  190.  
    jg.useDefaultPrettyPrinter();
  191.  
    }
  192.  
    // 04-Mar-2010, tatu: How about type we were given? (if any)
  193.  
    JavaType rootType = null;
  194.  
     
  195.  
    if (genericType != null && value != null) {
  196.  
    /* 10-Jan-2011, tatu: as per [JACKSON-456], it's not safe to just force root
  197.  
    * type since it prevents polymorphic type serialization. Since we really
  198.  
    * just need this for generics, let's only use generic type if it's truly
  199.  
    * generic.
  200.  
    */
  201.  
    if (genericType.getClass() != Class.class) { // generic types are other impls of 'java.lang.reflect.Type'
  202.  
    /* This is still not exactly right; should root type be further
  203.  
    * specialized with 'value.getClass()'? Let's see how well this works before
  204.  
    * trying to come up with more complete solution.
  205.  
    */
  206.  
    rootType = writer.getTypeFactory().constructType(genericType);
  207.  
    /* 26-Feb-2011, tatu: To help with [JACKSON-518], we better recognize cases where
  208.  
    * type degenerates back into "Object.class" (as is the case with plain TypeVariable,
  209.  
    * for example), and not use that.
  210.  
    */
  211.  
    if (rootType.getRawClass() == Object.class) {
  212.  
    rootType = null;
  213.  
    }
  214.  
    }
  215.  
    }
  216.  
     
  217.  
    // Most of the configuration now handled through EndpointConfig, ObjectWriter
  218.  
    // but we may need to force root type:
  219.  
    if (rootType != null) {
  220.  
    writer = writer.withType(rootType);
  221.  
    }
  222.  
    value = endpoint.modifyBeforeWrite(value);
  223.  
    writer.writeValue(jg, value);
  224.  
    } finally {
  225.  
    jg.close();
  226.  
    }
  227.  
    }
  228.  
     
  229.  
     
  230.  
    }
学新通

1、在service中加入以上类;

2、在service的META-INF下创建services文件夹

3、在services文件夹下创建javax.ws.rs.ext.Providers文件

4、javax.ws.rs.ext.Providers中写入上述类的引用

引用的依赖:

  1.  
    <!-- 使用json序列化 -->
  2.  
    <dependency>
  3.  
    <groupId>org.jboss.resteasy</groupId>
  4.  
    <artifactId>resteasy-jackson2-provider</artifactId>
  5.  
    <version>3.0.17.Final</version>
  6.  
    </dependency>
  7.  
     
  8.  
    <dependency>
  9.  
    <groupId>com.fasterxml.jackson.core</groupId>
  10.  
    <artifactId>jackson-annotations</artifactId>
  11.  
    <version>2.11.0</version>
  12.  
    </dependency>
  13.  
    <dependency>
  14.  
    <groupId>com.fasterxml.jackson.core</groupId>
  15.  
    <artifactId>jackson-core</artifactId>
  16.  
    <version>2.11.0</version>
  17.  
    </dependency>
  18.  
    <dependency>
  19.  
    <groupId>com.fasterxml.jackson.core</groupId>
  20.  
    <artifactId>jackson-databind</artifactId>
  21.  
    <version>2.11.0</version>
  22.  
    </dependency>
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgehhfk
系列文章
更多 icon
同类精品
更多 icon
继续加载