[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);}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/640760.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

苹果眼镜(Vision Pro)的开发者指南(4)——ARKit插件

该平台运用ARKit算法,处理持久性、世界映射、分割、遮罩和环境照明等功能。这些算法持续运行,使得应用和游戏在共享空间中自动受益于ARKit。一旦应用启动专用的Full Space,它便能利用ARKit API,完美融合虚拟与现实世界。 在此,我们将深入探讨这个框架如何重新定义交互体验…

React16源码: React中的reconcileChildren的源码实现

reconcileChildren 1 &#xff09;概述 在更新了一个节点之后&#xff0c;拿到它的props.children要根据这个children里面的 ReactElement 来去创建子树的所有的 fiber 对象要根据 props.children 来生成 fiber 子树&#xff0c;然后判断 fiber 对象它是否是可以复用的 因为我…

QT实现USB通讯

一.概述 QT实现USB通讯这里主要介绍两种方法&#xff0c;一种是通过libusb库来实现usb通讯&#xff0c;一种是通过hidapi库实现通信。 1.介绍libusb库 libusb 是一个 C 库&#xff0c;提供对 USB 设备的通用访问。 可移植的&#xff1a;使用单个跨平台API&#xff0c;它可以…

一、防御保护---信息安全概述

一、网络安全防御---信息安全概述 1.信息安全现状及挑战1.1 网络空间安全市场在中国&#xff0c;潜力无穷1.2 数字化时代威胁升级1.3 传统安全防护逐步失效1.4 安全风险能见度不足1.5 缺乏自动化防御手段1.6 网络安全监管标准愈发严苛 2.信息安全概述2.1 简介2.2 常见的网络安全…

Java 设计者模式以及与Spring关系(四) 代理模式

目录 简介: 23设计者模式以及重点模式 代理模式&#xff08;Proxy Pattern&#xff09; 静态代理示例 spring中应用 动态代理 1.基于JDK的动态代理 target.getClass().getInterfaces()作用 内名内部类写法(更简洁&#xff0c;但不推荐) 2.基于CGLIB实现 spring中应用 …

uniapp使用自定义组件

tt.vue中使用video-player组件 用到的目录如下&#xff1a; pages.json {"path": "pages/Tabbar/tt/tt","style": {"navigationBarTitleText": "","enablePullDownRefresh": false,// 使用自定义组件"using…

C++ 类定义

C 类定义 定义一个类需要使用关键字 class&#xff0c;然后指定类的名称&#xff0c;并类的主体是包含在一对花括号中&#xff0c;主体包含类的成员变量和成员函数。 定义一个类&#xff0c;本质上是定义一个数据类型的蓝图&#xff0c;它定义了类的对象包括了什么&#xff0…

【论文阅读笔记】Swin-Unet: Unet-like Pure Transformer for Medical Image Segmentation

1.介绍 Swin-Unet: Unet-like Pure Transformer for Medical Image Segmentation Swin-Unet&#xff1a;用于医学图像分割的类Unet纯Transformer 2022年发表在 Computer Vision – ECCV 2022 Workshops Paper Code 2.摘要 在过去的几年里&#xff0c;卷积神经网络&#xff…

14.任务管理系统

功能描述 角色 管理员、项目经理、员工 管理员功能 录入基础信息、用户信息。 项目经理功能 登陆系统录入项目信息、需求信息、拆解任务、分配任务 员工功能 登陆系统&#xff0c;查看任务、记录任务工作日志、完成任务。 数据模型 项目表 t_jd_project 字段名 类型 …

x-cmd pkg | dasel - JSON、YAML、TOML、XML、CSV 数据的查询和修改工具

目录 简介首次用户快速实验指南基本功能性能特点竞品进一步探索 简介 dasel&#xff0c;是数据&#xff08;data&#xff09;和 选择器&#xff08;selector&#xff09;的简写&#xff0c;该工具使用选择器查询和修改数据结构。 支持 JSON&#xff0c;YAML&#xff0c;TOML&…

CentOS 7 安装配置MySQL

目录 一、安装MySQL​编辑​编辑 1、检查MySQL是否安装及版本信息​编辑 2、卸载 2.1 rpm格式安装的mysql卸载方式 2.2 二进制包格式安装的mysql卸载 3、安装 二、配置MySQL 1、修改MySQL临时密码 2、允许远程访问 2.1 修改MySQL允许任何人连接 2.2 防火墙的问题 2…

大哈的变换迷宫的题解

目录 原题描述&#xff1a; 【题目描述】 【输入格式】 【输出格式】 【样例输入】 【样例输出】 【数据范围】 题目大意&#xff1a; 主要思路&#xff1a; 代码code&#xff08;附有注释&#xff09; 时间限制: 1000ms 空间限制: 524288kB 原题描述&#xff1a; …

【webrtc】跟webrtc学时间戳、序号类型转换

间隔ms src\modules\congestion_controller\remb_throttler.ccnamespace {constexpr TimeDelta kRembSendInterval = TimeDelta::Millis(200); } // namespace百分比的处理 src\modules\congestion_controller\remb_throttler.ccvoid RembT

行业-职业-大全-JSON

数据来源&#xff1a; https://blog.csdn.net/wjwABCDEFG/article/details/115669504 已将JSON进行格式处理 [{"Industry": "人事/行政/后勤","Occupations": ["人事专员/助理","人事信息系统(HRIS)管理","人事总监…

kotlin 简单实现实体类的Parcelable序列化接口

以前用Java代码实现Parcelable序列化接口&#xff0c;需要在实体类里面写一堆代码&#xff0c;麻烦得很&#xff0c;现在用kotlin开发安卓APP&#xff0c;只需2步就可以实现&#xff0c;这里记录下&#xff1b; 1. 在模块的build.gradle文件如下配置&#xff1a; apply plugi…

TCP三握四挥(面试需要)

TCP建立连接需要三次握手过程&#xff0c;关闭连接需要四次挥手过程 三次握手 从图中可以看出&#xff0c;客户端在发起connect时&#xff0c;会发起第一次和第三次握手。服务端在接收客户端连接时&#xff0c;会发起第二次握手。 这三次握手&#xff0c;都会通过SYNACK的方式…

基于ChatGPT4+Python近红外光谱数据分析及机器学习与深度学习建模教程

详情点击链接&#xff1a;基于ChatGPT4Python近红外光谱数据分析及机器学习与深度学习建模教程 第一&#xff1a;GPT4 1、ChatGPT&#xff08;GPT-1、GPT-2、GPT-3、GPT-3.5、GPT-4模型的演变&#xff09; 2、ChatGPT对话初体验 3、GPT-4与GPT-3.5的区别&#xff0c;以及与…

20240122在WIN10+GTX1080下使用字幕小工具V1.2的使用总结(whisper)

20240122在WIN10GTX1080下使用字幕小工具V1.2的使用总结 2024/1/22 19:52 结论&#xff1a;这个软件如果是习作&#xff0c;可以打101分&#xff0c;功能都实现了。 如果作为商业软件/共享软件&#xff0c;在易用性等方面&#xff0c;可能就只能有70分了。 【百分制】 可选的改…

2017年认证杯SPSSPRO杯数学建模A题(第二阶段)安全的后视镜全过程文档及程序

2017年认证杯SPSSPRO杯数学建模 A题 安全的后视镜 原题再现&#xff1a; 汽车后视镜的视野对行车安全非常重要。一般来说&#xff0c;汽车的后视镜需要有良好的视野范围&#xff0c;以便驾驶员能够全面地了解车后方的道路情况。同时&#xff0c;后视镜也要使图像的畸变尽可能…

idea编译打包前端vue项目

网上download了一个前端vue项目 第一次接触前端记录一下编译打包遇到的问题 1、idea前端项目打包一般是依赖 <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.0…