Spring Boot 配置 FastJson

jackson

Spring Boot 自带的序列化工具是 jackson

最简单使用

java 复制代码
@RestController  
public class PingController {  
    @GetMapping("ping")  
    public Map<String, Object> ping() {  
        Map<String, Object> map = new HashMap<>();  
        Date date = new Date();
        map.put("data", "pong");  
        map.put("date", date)
        return map;  
    }  
}

配置 jackson 序列行为

yaml 复制代码
spring:  
  jackson:  
    date-format: yyyy-MM-dd HH:mm:ss  # 时间格式化方式  
    time-zone: Asia/Shanghai  # 时区  
    serialization:  
      write-dates-as-timestamps: false # 时间戳

响应结果:

json 复制代码
{
	"date": "2024-10-13 13:55:16",
	"data": "pong"
}

fastjson

Spring Boot 3.x 中配置 fastjson

安装依赖

gradle 复制代码
implementation "com.alibaba:fastjson:1.2.83"

配置 fastjson 有三种:

  1. 利用 Spring Boot 自动配置机制配置
  2. 使用 @EnableWebMvc 注解,Spring Boot 自动配置机制会失效
  3. 使用 FastJsonHttpMessageConverter 替换 jackson

使用 Spring Boot 自动配置机制

java 复制代码
@Configuration  
public class WebConfig implements WebMvcConfigurer {  
    @Bean  
    public FastJsonConfig fastJsonConfig() {  
        FastJsonConfig config = new FastJsonConfig();  
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");  // 时间格式化
        config.setSerializerFeatures(SerializerFeature.PrettyFormat); // 美化输出
        config.setSerializeConfig(SerializeConfig.globalInstance);   // 表示开启全局序列化配置
        return config;  
    }  
  
    @Override  
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {  
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();  
        converter.setDefaultCharset(StandardCharsets.UTF_8);  // 设置字符集为 UTF-8   
        List<MediaType> mediaTypes = Collections.singletonList(MediaType.APPLICATION_JSON);  
        fastConverter.setSupportedMediaTypes(mediaTypes);  // 转换器只处理 application/json 类型的请求和响应
        fastConverter.setFastJsonConfig(fastJsonConfig());  
	    converters.add(0, fastConverter);  // 将 fastjson 转换器转换器添加到首位,确保优先使用 fastjson 转换器
    }
}

使用 @EnableWebMvc 注解

java 复制代码
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {  
    @Bean  
    public FastJsonConfig fastJsonConfig() {  
        FastJsonConfig config = new FastJsonConfig();  
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");  // 时间格式化
        config.setSerializerFeatures(SerializerFeature.PrettyFormat);  
        config.setSerializeConfig(SerializeConfig.globalInstance);  
        return config;  
    }  
  
    @Override  
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {  
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();  
        fastConverter.setDefaultCharset(StandardCharsets.UTF_8);  // 设置字符集为 UTF-8   
        List<MediaType> mediaTypes = Collections.singletonList(MediaType.APPLICATION_JSON);  
        fastConverter.setSupportedMediaTypes(mediaTypes);  // 转换器只处理 application/json 类型的请求和响应
        fastConverter.setFastJsonConfig(fastJsonConfig());  
	    converters.add(fastConverter);  // fastjson 会被添加到转换器列表的末尾,需要配置 EnableWebMvc 使用
    }
}

这两方式的区别就在于使用 converters.add(converter) 会将 fastjson 转换器添加到转换器列表的末尾,Spring Boot 内置的 jackson 转换器会优先执行

所以需要在类上面添加上 @EnableWebMvc 的注解,让 Spring Boot 内置的转换器失效

使用 FastJsonHttpMessageConverter 替换 jackson

java 复制代码
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Bean
    public FastJsonHttpMessageConverter responseBodyConverter() {
        FastJsonConfig config = new FastJsonConfig();
        config.setDateFormat("yyyy-MM-dd HH:mm:ss");
        config.setSerializerFeatures(SerializerFeature.PrettyFormat);
        config.setSerializeConfig(SerializeConfig.globalInstance);
          
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        fastConverter.setFastJsonConfig(config);
        return fastConverter;
    }
}

与前面两种方式的区别是,这种方式是完全替代 jackson ,而前两种方式是 jacksonfastjson 共存的

相关推荐
St_rive3 小时前
Page Object设计模式
java·开发语言·设计模式
风流 少年3 小时前
Spring AI 2.0:SSE
java·人工智能·spring
凤山老林4 小时前
精细化流量治理:Spring Boot 动态特性开关与灰度发布体系
java·spring boot·后端
Aphelios3806 小时前
一次锁内网络IO引发的Tomcat线程池“饿死”事故
java·开发语言·spring boot·elasticsearch·tomcat·网络io阻塞·线程池耗尽
不可求~6 小时前
C++ std::string_view 不是字符串:从悬空引用到安全用法
java·开发语言·c++
Lam Tang6 小时前
APS 系列文章10
java·代理模式
考虑考虑7 小时前
Excel导入时产生特殊字符处理
java·后端·java ee
Scabbards_7 小时前
面试Leetcode - Heap 堆
java·leetcode·面试
程序员清风8 小时前
专业再升级!程序员专属显示器明基RD280UG上手实测!
java·后端·面试
我命由我123459 小时前
RxJava - 冷数据流与热数据流
android·java·java-ee·android studio·rxjava·android-studio·android runtime