跳至主要內容

Fastjson1 升级 Fastjson2

Jin大约 1 分钟

Fastjson1 升级 Fastjson2

参考

1、依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
    <!--去掉 jackson 依赖-->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-json</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<properties>
    <fastjson2.version>2.0.51</fastjson2.version>
</properties>

<dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2</artifactId>
    <version>${fastjson2.version}</version>
</dependency>

<dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2-extension-spring6</artifactId>
    <version>${fastjson2.version}</version>
</dependency>

2、配置

import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

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


@Configuration
public class Fastjson2Config implements WebMvcConfigurer {
    /**
     * 配置fastjson输出格式
     **/
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

//        添加fastjson转换器
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
//        配置fastjson
        converter.setFastJsonConfig(getFastJsonConfig());

        List<MediaType> mediaTypes = new ArrayList<>();
//        添加支持的媒体类型
//        解决中文乱码问题,相当于在 Controller 上的@RequestMapping 中加了个属性 produces ="application/json"
        mediaTypes.add(MediaType.APPLICATION_JSON);
        mediaTypes.add(MediaType.APPLICATION_ATOM_XML);
        mediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
        mediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
        mediaTypes.add(MediaType.APPLICATION_PDF);
        mediaTypes.add(MediaType.APPLICATION_RSS_XML);
        mediaTypes.add(MediaType.APPLICATION_XHTML_XML);
        mediaTypes.add(MediaType.APPLICATION_XML);
        mediaTypes.add(MediaType.IMAGE_GIF);
        mediaTypes.add(MediaType.IMAGE_JPEG);
        mediaTypes.add(MediaType.IMAGE_PNG);
        mediaTypes.add(MediaType.TEXT_EVENT_STREAM);
        mediaTypes.add(MediaType.TEXT_HTML);
        mediaTypes.add(MediaType.TEXT_MARKDOWN);
        mediaTypes.add(MediaType.TEXT_PLAIN);
        mediaTypes.add(MediaType.TEXT_XML);
//        在convert中添加配置信息
        converter.setSupportedMediaTypes(mediaTypes);
//        将convert添加到converters
        converters.add(0, converter);
    }

    /**
     * <h2>配置fastjson2</h2>
     **/
    private static FastJsonConfig getFastJsonConfig() {
        FastJsonConfig config = new FastJsonConfig();
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        config.setCharset(StandardCharsets.UTF_8);
        config.setWriterFeatures(
                //json格式化
                JSONWriter.Feature.PrettyFormat,
                //输出map中value为null的数据,保留 map 空的字段
                JSONWriter.Feature.WriteMapNullValue,
                //输出boolean 为 false,将 Boolean 类型的 null 转成 false
                JSONWriter.Feature.WriteNullBooleanAsFalse,
                //输出list 为 []
                JSONWriter.Feature.WriteNullListAsEmpty,
                //输出number 为 0
                JSONWriter.Feature.WriteNullNumberAsZero,
                //输出字符串 为 "" ,将 String 类型的 null 转成""
                JSONWriter.Feature.WriteNullStringAsEmpty,
                //对map进行排序
                JSONWriter.Feature.SortMapEntriesByKeys
        );
        return config;
    }

}

3、全局替换

JSON

import com.alibaba.fastjson.JSON;
// 替换成
import com.alibaba.fastjson2.JSON;

JSONObject

import com.alibaba.fastjson.JSONObject;
// 替换成
import com.alibaba.fastjson2.JSONObject;

JSONArray

import com.alibaba.fastjson.JSONArray;
//替换成
import com.alibaba.fastjson2.JSONArray;

JSONField

import com.alibaba.fastjson.annotation.JSONField;
// 替换成
import com.alibaba.fastjson2.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType;
// 替换成
import com.alibaba.fastjson2.annotation.JSONType;
import com.alibaba.fastjson.JSONException;
// 替换成
import com.alibaba.fastjson2.JSONException;

格式化写法变更

PrettyFormat

String result = JSON.toJSONString(jsonObject, SerializerFeature.PrettyFormat);
// 替换成
String result = JSON.toJSONString(jsonObject, JSONWriter.Feature.PrettyFormat);

SerializerFeature.PrettyFormat
// 替换成
JSONWriter.Feature.PrettyFormat

WriteMapNullValue

SerializerFeature.WriteMapNullValue

// 替换成
JSONWriter.Feature.WriteMapNullValue

parseArray

JSONObject.parseArray
// 替换成
JSON.parseArray
贡献者: Jin