[SpringBoot2.6.13]FastJsonHttpMessageConverter不生效

2024-01-23 04:28

本文主要是介绍[SpringBoot2.6.13]FastJsonHttpMessageConverter不生效,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 错误描述
    • 问题分析
      • 打印目前所有的消息处理器
      • 寻找适配版本
      • 消息解释器加载顺序
    • 错误原因
    • 正确写法
      • 使用最新版本fastjson(2024-1-22)
      • 配置fastjson2消息转换器(保留系统原消息转换器)
      • 替换消息转换器配置fastjson2

错误描述

采用@Bean的方式配置FastJsonHttpMessageConverter消息解释器,实测在【SpringBoot2.6.13】未生效
但是在【SpringBoot2.1.4.RELEASE】版本中正常
2.1.4.RELEASE中引入如下

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

配置如下

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;@Beanpublic HttpMessageConverters fastJsonHttpMessageConverters() {FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();FastJsonConfig fastJsonConfig = new FastJsonConfig();fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,SerializerFeature.QuoteFieldNames,SerializerFeature.WriteEnumUsingName,SerializerFeature.DisableCircularReferenceDetect);List<MediaType> fastMediaTypes = new ArrayList<>();fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);fastConverter.setSupportedMediaTypes(fastMediaTypes);fastConverter.setFastJsonConfig(fastJsonConfig);return new HttpMessageConverters(fastConverter);}

问题分析

打印目前所有的消息处理器

通过打印消息处理器,发现配置并未成功

public class MvcConfig implements WebMvcConfigurer {@Overridepublic void extendMessageConverters(List<HttpMessageConverter<?>> converters) {for (HttpMessageConverter<?> messageConverter : converters) {System.out.println(messageConverter);}}
}

得到如下输出日志

org.springframework.http.converter.ByteArrayHttpMessageConverter@1ddf42dd
org.springframework.http.converter.StringHttpMessageConverter@5c1c9881
org.springframework.http.converter.ResourceHttpMessageConverter@1c18ee69
org.springframework.http.converter.ResourceRegionHttpMessageConverter@2f99d8c
org.springframework.http.converter.xml.SourceHttpMessageConverter@65d7eea4
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@5d37aa0f
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@6076c66
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@485c84d7

寻找适配版本

官网可知,fastjson早已停止更新,新版本需使用fastjson2
在 Spring 中集成 Fastjson2 | fastjson2 (alibaba.github.io)
引入如下

<dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2-extension-spring5</artifactId><version>2.0.43</version>
</dependency>

官网得到如下配置

@Configuration
@EnableWebMvc
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();//自定义配置...FastJsonConfig config = new FastJsonConfig();config.setDateFormat("yyyy-MM-dd HH:mm:ss");config.setReaderFeatures(JSONReader.Feature.FieldBased, JSONReader.Feature.SupportArrayToBean);config.setWriterFeatures(JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.PrettyFormat);converter.setFastJsonConfig(config);converter.setDefaultCharset(StandardCharsets.UTF_8);converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));converters.add(0, converter);}
}

消息解释器加载顺序

需要将FastJsonHttpMessageConverter配置为第一位消息处理器才能得到输出
其测试过程如下

converters.add(converter);

此时得到输出为

org.springframework.http.converter.ByteArrayHttpMessageConverter@1ddf42dd
org.springframework.http.converter.StringHttpMessageConverter@5c1c9881
org.springframework.http.converter.ResourceHttpMessageConverter@1c18ee69
org.springframework.http.converter.ResourceRegionHttpMessageConverter@2f99d8c
org.springframework.http.converter.xml.SourceHttpMessageConverter@65d7eea4
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@5d37aa0f
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@6076c66
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@485c84d7
com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter@1224e1b6

实测未生效

{"code": 200,"msg": "请求成功","data": {"loginName": null,"userName": "柒杉","userCode": null,"loginDate": 1705547281595}
}

修改为

converters.add(0, converter);

得到输出

com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter@2a667f44
org.springframework.http.converter.ByteArrayHttpMessageConverter@53ba7997
org.springframework.http.converter.StringHttpMessageConverter@3f6f9cef
org.springframework.http.converter.ResourceHttpMessageConverter@61dd1c3d
org.springframework.http.converter.ResourceRegionHttpMessageConverter@7858d31d
org.springframework.http.converter.xml.SourceHttpMessageConverter@782e6b40
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@3b65084e
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@32d0d7eb
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@cae2a97

错误原因

在高版本中需要采用最新的fastjson2配置

正确写法

使用最新版本fastjson(2024-1-22)

<dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2-extension-spring5</artifactId><version>2.0.43</version>
</dependency>

配置fastjson2消息转换器(保留系统原消息转换器)

        @Overridepublic void extendMessageConverters(List<HttpMessageConverter<?>> converters) {// 创建 FastJson 的消息转换器实例FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();// 创建 FastJson 的配置实例FastJsonConfig config = new FastJsonConfig();// 设置时间类型日期格式config.setDateFormat("yyyy-MM-dd HH:mm:ss");config.setReaderFeatures(// 基于字段序列化,如果不配置,会默认基于public的field和getter方法序列化。// 配置后,会基于非static的field(包括private)做序列化。JSONReader.Feature.FieldBased,// 支持数据映射的方式JSONReader.Feature.SupportArrayToBean);config.setWriterFeatures(// 显示null与空字符串//                JSONWriter.Feature.WriteMapNullValue,// 格式化输出JSONWriter.Feature.PrettyFormat,// 将Long序列化为StringJSONWriter.Feature.WriteLongAsString);// 将序列化配置设置到 FastJson 配置中converter.setFastJsonConfig(config);converter.setDefaultCharset(StandardCharsets.UTF_8);// 创建一个媒体类型,表示支持 JSON 格式,并使用 UTF-8 编码List<MediaType> fastMediaTypes = new ArrayList<>();MediaType utf8MediaType = new MediaType(MediaType.APPLICATION_JSON, StandardCharsets.UTF_8);fastMediaTypes.add(utf8MediaType);converter.setSupportedMediaTypes(fastMediaTypes);// 将 FastJson 消息转换器添加到 Spring Boot 的消息转换器列表中,位置是第一个,这样确保它优先于其他消息转换器converters.add(0, converter);}

替换消息转换器配置fastjson2

@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {// 创建 FastJson 的消息转换器实例FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();// 创建 FastJson 的配置实例FastJsonConfig config = new FastJsonConfig();// 设置时间类型日期格式config.setDateFormat("yyyy-MM-dd HH:mm:ss");config.setReaderFeatures(// 基于字段序列化,如果不配置,会默认基于public的field和getter方法序列化。// 配置后,会基于非static的field(包括private)做序列化。JSONReader.Feature.FieldBased,// 支持数据映射的方式JSONReader.Feature.SupportArrayToBean);config.setWriterFeatures(// 显示null与空字符串//                JSONWriter.Feature.WriteMapNullValue,// 格式化输出JSONWriter.Feature.PrettyFormat,// 将Long序列化为StringJSONWriter.Feature.WriteLongAsString);// 将序列化配置设置到 FastJson 配置中converter.setFastJsonConfig(config);converter.setDefaultCharset(StandardCharsets.UTF_8);// 创建一个媒体类型,表示支持 JSON 格式,并使用 UTF-8 编码List<MediaType> fastMediaTypes = new ArrayList<>();MediaType utf8MediaType = new MediaType(MediaType.APPLICATION_JSON, StandardCharsets.UTF_8);fastMediaTypes.add(utf8MediaType);converter.setSupportedMediaTypes(fastMediaTypes);// 将 FastJson 消息转换器添加到 Spring Boot 的消息转换器列表中,位置是第一个,这样确保它优先于其他消息转换器converters.add(0, converter);}

这篇关于[SpringBoot2.6.13]FastJsonHttpMessageConverter不生效的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/635222

相关文章

Feign Client超时时间设置不生效的解决方法

《FeignClient超时时间设置不生效的解决方法》这篇文章主要为大家详细介绍了FeignClient超时时间设置不生效的原因与解决方法,具有一定的的参考价值,希望对大家有一定的帮助... 在使用Feign Client时,可以通过两种方式来设置超时时间:1.针对整个Feign Client设置超时时间

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3

Flask解决指定端口无法生效问题

《Flask解决指定端口无法生效问题》文章讲述了在使用PyCharm开发Flask应用时,启动地址与手动指定的IP端口不一致的问题,通过修改PyCharm的运行配置,将Flask项目的运行模式从Fla... 目录android问题重现解决方案问题重现手动指定的IP端口是app.run(host='0.0.

Linux:alias如何设置永久生效

《Linux:alias如何设置永久生效》在Linux中设置别名永久生效的步骤包括:在/root/.bashrc文件中配置别名,保存并退出,然后使用source命令(或点命令)使配置立即生效,这样,别... 目录linux:alias设置永久生效步骤保存退出后功能总结Linux:alias设置永久生效步骤

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

13 transition数组的动画使用

划重点 动画:transitiontransition-group :数组动画数组的 添加 / 删除 豆腐粉丝汤 清淡又健康 <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><me

【CTF Web】BUUCTF Upload-Labs-Linux Pass-13 Writeup(文件上传+PHP+文件包含漏洞+PNG图片马)

Upload-Labs-Linux 1 点击部署靶机。 简介 upload-labs是一个使用php语言编写的,专门收集渗透测试和CTF中遇到的各种上传漏洞的靶场。旨在帮助大家对上传漏洞有一个全面的了解。目前一共20关,每一关都包含着不同上传方式。 注意 1.每一关没有固定的通关方法,大家不要自限思维! 2.本项目提供的writeup只是起一个参考作用,希望大家可以分享出自己的通关思路

Chapter 13 普通组件的注册使用

欢迎大家订阅【Vue2+Vue3】入门到实践 专栏,开启你的 Vue 学习之旅! 文章目录 前言一、组件创建二、局部注册三、全局注册 前言 在 Vue.js 中,组件是构建应用程序的基本单元。本章详细讲解了注册和使用 Vue 的普通组件的两种方式:局部注册和全局注册。 本篇文章参考黑马程序员 一、组件创建 ①定义 Vue 组件是一种具有特定功能的 Vue 实

VMware Fusion Pro 13 Mac版虚拟机 安装Win11系统教程

Mac分享吧 文章目录 Win11安装完成,软件打开效果一、VMware安装Windows11虚拟机1️⃣:准备镜像2️⃣:创建虚拟机3️⃣:虚拟机设置4️⃣:安装虚拟机5️⃣:解决连不上网问题 安装完成!!! Win11安装完成,软件打开效果 一、VMware安装Windows11虚拟机 首先确保自己的mac开启了网络共享。不然虚拟机连不上👀的 1️⃣:准备镜像

华为 HCIP-Datacom H12-821 题库 (13)

有需要题库的可以看主页置顶 1.可以携带外部路由的 tag 标签信息的是以下哪一类 LSA? A、4 类 LSA B、5 类 LSA  C、3 类 LSA  D、2 类 LSA 答案:B 解析: 暂无解析 2..两台路由器直连,并设定网络类型为 p2p 建立OSPF 邻居。那么两台路由器传输 OSPF 报文的目的 IP 地址是以下哪一项? A、使用组播地址 224.0.0.6 B