码迷,mamicode.com
首页 > Web开发 > 详细

fastjson数据返回配置

时间:2019-11-11 18:26:15      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:pes   mvc   格式化输出   tpm   apn   dna   dia   config   otc   

阿里的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>

 

 

fastjson数据返回配置

标签:pes   mvc   格式化输出   tpm   apn   dna   dia   config   otc   

原文地址:https://www.cnblogs.com/yrjns/p/11766440.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!