• fastjson数据返回配置


    阿里的fastjson 包升级后,可能导致返回的json 数据,字段为null时不显示等问题

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.9.13</version>
    </dependency>
    @Configuration
    public class FastjsonConfiguration {
    
    
      @Bean
      public HttpMessageConverters fastjsonConverter() {
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //自定义格式化输出
        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero);
    
        FastJsonHttpMessageConverter4 fastjson = new FastJsonHttpMessageConverter4();
        fastjson.setFastJsonConfig(fastJsonConfig);
        return new HttpMessageConverters(fastjson);
      }
    
    }
    SerializerFeature  类的一些枚举值
        WriteMapNullValue,
    
        WriteNullListAsEmpty //  将list的null转成 []
    
        WriteNullStringAsEmpty // 将string的null转成 ""
    
        WriteNullNumberAsZero
    
        WriteNullBooleanAsFalse,
    
        PrettyFormat,
    
        WriteClassName,
    
        WriteDateUseDateFormat,
    
        NotWriteRootClassName,
    
        BeanToArray,
    
        WriteNonStringKeyAsString,
    
        NotWriteDefaultValue,
        
        BrowserSecure,
        
        IgnoreNonFieldGetter,
    
        WriteNonStringValueAsString,
        
    
      

    springMVC 中设置

                <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> 
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                            <value>application/json</value>
                        </list>
                    </property>
                    <property name="features">
                        <list>
                            <value>WriteMapNullValue</value>
                            <value>QuoteFieldNames</value>
                        </list>
                    </property>
                   <!--  <property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"/> -->
                </bean>

    2、脱敏设置

    1 ) 自定义 SensentiveField 类 实现  ObjectSerializer,并重写其方法
    2) @JSONField(serializeUsing =SensentiveField.class) 实体类的属性加上 注解

  • 相关阅读:
    mongodb的学习-1-NoSQL
    mongodb的学习-3-在Mac上的安装配置
    面试题之算法与编程
    笔试题之javaweb
    笔试题之j2ee
    笔试题之代码查错
    笔试题之java基础
    javaweb笔记分享
    过滤器入门
    jsp入门
  • 原文地址:https://www.cnblogs.com/yrjns/p/11766440.html
Copyright © 2020-2023  润新知