美文网首页
Springboot2.0配置fastjson

Springboot2.0配置fastjson

作者: 谁在烽烟彼岸 | 来源:发表于2020-01-08 14:18 被阅读0次
package com.example.web.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:liupengfei
 * @createTime: 2020/1/8 10:55 上午
 * @discription:
 * @package: com.example.web.config
 * @project: test_project
 **/
@Configuration
public class JsonConfig extends WebMvcConfigurationSupport {
    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);

        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteEnumUsingName,
                SerializerFeature.WriteMapNullValue
        );
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        fastJsonConfig.setSerializeFilters(

        );
        fastJsonConfig.setCharset(
                StandardCharsets.UTF_8
        );
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(fastJsonHttpMessageConverter);

    }
}

第二种写法

package com.example.web.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:liupengfei
 * @createTime: 2020/1/8 10:55 上午
 * @discription:
 * @package: com.example.web.config
 * @project: test_project
 **/
@Configuration
public class JsonConfig {
    @Bean
    public HttpMessageConverters httpMessageConverter() {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteEnumUsingName,
                SerializerFeature.WriteMapNullValue
        );
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        fastJsonConfig.setSerializeFilters(

        );
        fastJsonConfig.setCharset(
                StandardCharsets.UTF_8
        );
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        return new HttpMessageConverters(fastJsonHttpMessageConverter);
    }
}

fastjson生成json时Null属性不显示

  • Fastjson的SerializerFeature序列化属性 --来自oschina bfleeee博客
  • QuoteFieldNames———-输出key时是否使用双引号,默认为true
  • WriteMapNullValue——–是否输出值为null的字段,默认为false
  • WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null
  • WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null
  • WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null
  • WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null。

相关文章

网友评论

      本文标题:Springboot2.0配置fastjson

      本文链接:https://www.haomeiwen.com/subject/mvcsactx.html