Spring Boot使用FastJson

Laughing
2019-07-21 / 0 评论 / 1,334 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2024年03月18日,已超过341天没有更新,若内容或图片失效,请留言反馈。

什么是FastJson?

fastJson是阿里巴巴旗下的一个开源项目之一,顾名思义它专门用来做快速操作Json的序列化与反序列化的组件。它是目前json解析最快的开源组件没有之一!在这之前jaskJson是命名为快速操作json的工具,而当阿里巴巴的fastJson诞生后jaskjson就消声匿迹了,不过目前很多项目还在使用。

maven依赖

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-databind</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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

[alt type="success"]fastjson虽然是阿里巴巴出品的,但是最近爆出很多bug,所以大项目上还是慎重使用[/alt]

创建配置信息类

我们接下来创建一个FastJsonConfiguration配置信息类,添加@Configuration注解让SpringBoot自动加载类内的配置,有一点要注意我们继承了WebMvcConfigurerAdapter这个类,这个类是SpringBoot内部提供专门处理用户自行添加的配置,里面不仅仅包含了修改视图的过滤还有其他很多的方法,比如拦截器,过滤器,Cors配置等。

方式一:fastJson视图过滤配置

package cn.notemi.configuration;  
import com.alibaba.fastjson.serializer.SerializerFeature;  
import com.alibaba.fastjson.support.config.FastJsonConfig;  
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;  
import org.springframework.context.annotation.Configuration;  
import org.springframework.http.converter.HttpMessageConverter;  
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;  
import java.util.List;  
/** 
 * Title:FastJsonConfiguration 
 * Description:FastJson配置信息 
 * 
 * @author Flicker 
 * @create 2017-08-08 下午 4:33 
 **/  
@Configuration  
public class FastJsonConfiguration extends WebMvcConfigurerAdapter  
{  
    /** 
     * 修改自定义消息转换器 
     * @param converters 消息转换器列表 
     */  
    @Override  
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {  
        //调用父类的配置  
        super.configureMessageConverters(converters);  
        //创建fastJson消息转换器  
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();  
        //创建配置类  
        FastJsonConfig fastJsonConfig = new FastJsonConfig();  
        //修改配置返回内容的过滤  
        fastJsonConfig.setSerializerFeatures(  
                SerializerFeature.DisableCircularReferenceDetect,  
                SerializerFeature.WriteMapNullValue,  
                SerializerFeature.WriteNullStringAsEmpty  
        );  
        fastConverter.setFastJsonConfig(fastJsonConfig);  
        //将fastjson添加到视图消息转换器列表内  
        converters.add(fastConverter);  
    }  
}

方式二:自定义FastJsonHttpMessageConverter

@Configuration
public class FastJsonConfig {

    @Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        com.alibaba.fastjson.support.config.FastJsonConfig fastJsonConfig = new com.alibaba.fastjson.support.config.FastJsonConfig();
        fastJsonConfig.setDateFormat("yyyy-MM-dd");
        fastJsonConfig.setCharset(StandardCharsets.UTF_8);
        fastJsonConfig.setSerializerFeatures(
//                SerializerFeature.WriteClassName,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullStringAsEmpty
        );
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
//        解决中文乱码
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        return fastJsonHttpMessageConverter;
    }

}

fastJson配置实体调用setSerializerFeatures方法可以配置多个过滤方式,下面我们来介绍下常用的SerializerFeatures配置。

FastJson SerializerFeatures
WriteNullListAsEmptyList字段如果为null,输出为[],而非null
WriteNullStringAsEmpty : 字符类型字段如果为null,输出为"",而非null
DisableCircularReferenceDetect :消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环)
WriteNullBooleanAsFalseBoolean字段如果为null,输出为false,而非null
WriteMapNullValue:是否输出值为null的字段,默认为false

项目运行
我们使用了过滤,SerializerFeature.WriteNullStringAsEmpty,本该显示null,显示为"",所以成功使用。

0

评论 (0)

取消
  1. 头像
    Laughing 作者
    Windows 10 · Google Chrome

    画图

    回复
  2. 头像
    fire_snake
    MacOS · Google Chrome

    NB 支持

    回复