zoukankan      html  css  js  c++  java
  • SpringBoot整合FastJson(七)

    一、Maven依赖

          <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.33</version>
            </dependency>

    二、配置类

     1 @Configuration
     2 public class WebMvcConfig implements WebMvcConfigurer {
     3  4  5  6  7     /**
     8      * 使用fastjson代替jackson
     9      *
    10      * @param
    11      */
    12 13     @Override
    14     public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    15         configurer.enable();
    16     }
    17 18     @Override
    19 20     public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    21 22     /*
    23      先把JackSon的消息转换器删除.
    24      备注: (1)源码分析可知,返回json的过程为:
    25                 Controller调用结束后返回一个数据对象,for循环遍历conventers,找到支持application/json的HttpMessageConverter,然后将返回的数据序列化成json。
    26                 具体参考org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor的writeWithMessageConverters方法
    27            (2)由于是list结构,我们添加的fastjson在最后。因此必须要将jackson的转换器删除,不然会先匹配上jackson,导致没使用fastjson
    28 29     */
    30         for (int i = converters.size() - 1; i >= 0; i--) {
    31             if (converters.get(i) instanceof MappingJackson2HttpMessageConverter) {
    32                 converters.remove(i);
    33             }
    34         }
    35         FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    36         //自定义fastjson配置
    37         FastJsonConfig config = new FastJsonConfig();
    38         config.setSerializerFeatures(
    39                 SerializerFeature.WriteMapNullValue,        // 是否输出值为null的字段,默认为false,我们将它打开
    40                 SerializerFeature.WriteNullListAsEmpty,     // 将Collection类型字段的字段空值输出为[]
    41                 SerializerFeature.WriteNullStringAsEmpty,   // 将字符串类型字段的空值输出为空字符串
    42                 SerializerFeature.WriteNullNumberAsZero,    // 将数值类型字段的空值输出为0
    43                 SerializerFeature.WriteDateUseDateFormat,
    44                 SerializerFeature.DisableCircularReferenceDetect    // 禁用循环引用
    45 46         );
    47 48         fastJsonHttpMessageConverter.setFastJsonConfig(config);
    49 50         // 添加支持的MediaTypes;不添加时默认为*/*,也就是默认支持全部
    51 52         // 但是MappingJackson2HttpMessageConverter里面支持的MediaTypes为application/json
    53 54 55         List<MediaType> fastMediaTypes = new ArrayList<>();
    56         MediaType mediaType = MediaType.parseMediaType("text/html;charset=UTF-8");
    57         fastMediaTypes.add(mediaType);
    58         fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    59         fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
    60         // fastJsonHttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
    61         converters.add(fastJsonHttpMessageConverter);
    62 63     }
    64 65 }
     

    三、使用

    @JSONField(serialize=false)
    private String delFlag;    // 数据删除标记, 0-已删除 1-有效

     

  • 相关阅读:
    数据结构8.4_动态存储管理之伙伴系统
    http code码实验
    php问题
    对称加密和非对称加密
    公钥与私钥,HTTPS详解
    数字证书原理,公钥私钥加密原理
    简明 Nginx Location Url 配置笔记
    HTTP状态码精简版
    给你掰扯清楚什么是正向代理,什么是反向代理
    关键字
  • 原文地址:https://www.cnblogs.com/dalianpai/p/11685645.html
Copyright © 2011-2022 走看看