首页 > 其他分享 >Springboot 集成 Fastjson2

Springboot 集成 Fastjson2

时间:2023-02-23 16:33:06浏览次数:37  
标签:集成 Fastjson2 Springboot supportedMediaTypes MediaType APPLICATION add import JSO

Springboot 整合 Fastjson2
排除默认的Jackson
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <!-- 去掉Jackson依赖,用fastjson -->
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-json</artifactId>
        </exclusion>
    </exclusions>
</dependency>
引入指定依赖
<dependency>
  <groupId>com.alibaba.fastjson2</groupId>
  <artifactId>fastjson2</artifactId>
  <version>2.0.21</version>
</dependency>

<dependency>
  <groupId>com.alibaba.fastjson2</groupId>
  <artifactId>fastjson2-extension</artifactId>
  <version>2.0.21</version>
</dependency>
创建fastJsonConfig
package com.smile.learn.fastjson.config;

import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
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 com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter;

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

/**
 * @author: smile
 * @title: fastjson 配置
 * @projectName: fastjson
 * @description: fastjson 配置
 * @date: 2023/2/23 11:41 AM
 */
@Configuration
public class FastjsonConfig implements WebMvcConfigurer {
    /**
     * 配置fastjson输出格式
     **/
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 1. 配置fastjson
        FastJsonConfig config = new FastJsonConfig();

        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        config.setCharset(StandardCharsets.UTF_8);
        config.setWriterFeatures(
                JSONWriter.Feature.WriteNullListAsEmpty,
                //json格式化
                JSONWriter.Feature.PrettyFormat,
                //输出map中value为null的数据
                JSONWriter.Feature.WriteMapNullValue,
                //输出boolean 为 false
                JSONWriter.Feature.WriteNullBooleanAsFalse,
                //输出list 为 []
                JSONWriter.Feature.WriteNullListAsEmpty,
                //输出number 为 0
                JSONWriter.Feature.WriteNullNumberAsZero,
                //输出字符串 为 ""
                JSONWriter.Feature.WriteNullStringAsEmpty,
                //对map进行排序
                JSONWriter.Feature.MapSortField
        );

        // 2. 添加fastjson转换器
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        List<MediaType> supportedMediaTypes = new ArrayList<>();

        // 3. 添加支持的媒体类型
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
        supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
        supportedMediaTypes.add(MediaType.APPLICATION_PDF);
        supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_XML);
        supportedMediaTypes.add(MediaType.IMAGE_GIF);
        supportedMediaTypes.add(MediaType.IMAGE_JPEG);
        supportedMediaTypes.add(MediaType.IMAGE_PNG);
        supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
        supportedMediaTypes.add(MediaType.TEXT_HTML);
        supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
        supportedMediaTypes.add(MediaType.TEXT_PLAIN);
        supportedMediaTypes.add(MediaType.TEXT_XML);

        converter.setSupportedMediaTypes(supportedMediaTypes);

        //4 将convert添加到converters
        converter.setFastJsonConfig(config);
        converters.add(0,converter);
    }
}

标签:集成,Fastjson2,Springboot,supportedMediaTypes,MediaType,APPLICATION,add,import,JSO
From: https://www.cnblogs.com/ywjcqq/p/17148555.html

相关文章

  • SpringBoot21 - 数据层解决方案 SQL
    数据层解决方案-SQL​ SSMP整合的时候数据层解决方案涉及到了哪些技术?MySQL数据库与MyBatisPlus框架,后面又学了Druid数据源的配置,所以现在数据层解决方案可以说是Mysql......
  • SpringBoot20 - 测试
    测试​ 测试是保障程序正确性的唯一屏障,在企业级开发中更是不可缺少,但是由于测试代码往往不产生实际效益,所以一些小型公司并不是很关注,导致一些开发者从小型公司进入中大......
  • JPA在SpringBoot中简单使用
    前言在SpringBoot项目中可以与JPA进行搭配,这样会省很多的开发时间,以下为JPA的简单使用一、导入依赖<!--springbootjpa依赖--><dependency><groupId>org.spring......
  • SpringBoot17 - 常用计量单位绑定
    常用计量单位绑定​ 在前面的配置中,我们书写了如下配置值,其中第三项超时时间timeout描述了服务器操作超时时间,当前值是-1表示永不超时。servers:ip-address:192.168......
  • SpringBoot18 - 校验
    校验​ 在书写时由于无法感知模型类中的数据类型,就会出现类型不匹配的问题,比如代码中需要int类型,配置中给了非法的数值,例如写一个“a",这种数据肯定无法有效的绑定,还会引......
  • SpringBoot19 - 数据类型转换
    数据类型转换​ 先把问题描述一下,这位开发者连接数据库正常操作,但是运行程序后显示的信息是密码错误。java.sql.SQLException:Accessdeniedforuser'root'@'localho......
  • SpringBoot14 - 热部署
    热部署​ 什么是热部署?简单说就是你程序改了,现在要重新启动服务器,嫌麻烦?不用重启,服务器会自己悄悄的把更新后的程序给重新加载一遍,这就是热部署。​ 热部署的功能是如......
  • SpringBoot15 - @ConfigurationProperties绑定属性
    @ConfigurationProperties为使用@Bean声明的第三方bean绑定属性​ 在基础篇学习了@ConfigurationProperties注解,此注解的作用是用来为bean绑定属性的。开发者可以在yml配......
  • SpringBoot基础篇学习讲义
    SpringBoot文档更新日志版本更新日期操作描述v1.02021/11/14A基础篇前言​ 很荣幸有机会能以这样的形式和互联网上的各位小伙伴一起学......
  • SpringBoot11 - 各类配置优先级
    配置高级​ 关于配置在基础篇讲过一部分,基础篇的配置总体上来说就是让各位小伙伴掌握配置的格式。比如配置文件如何写啊,写好的数据如何读取啊,都是基础的语法级知识。在实......