FastJson2中FastJsonHttpMessageConverter找不到类问题

 问题描述 

如果你最近也在升级FastJson到FastJson2版本,而跟我一样也遇到了FastJsonHttpMessageConverter找不到类问题以及FastJsonConfig找不到问题,那么恭喜你,看完本文,安装完fastjson2、fastjson2-extension、fastjson2-extension-spring6这三个类库,你就可以成功使用新版FastJson2了。

旧代码

    @Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {converters.clear();//FastJsonHttpMessageConverterFastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();List<MediaType> fastMediaTypes = new ArrayList<>();fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);fastConverter.setSupportedMediaTypes(fastMediaTypes);FastJsonConfig fastJsonConfig = new FastJsonConfig();fastJsonConfig.setCharset(StandardCharsets.UTF_8);fastConverter.setFastJsonConfig(fastJsonConfig);//StringHttpMessageConverterStringHttpMessageConverter stringConverter = new StringHttpMessageConverter();stringConverter.setDefaultCharset(StandardCharsets.UTF_8);stringConverter.setSupportedMediaTypes(fastMediaTypes);converters.add(stringConverter);converters.add(fastConverter);}

 maven build error

 RCA调查

通过官方一个issue中我们发现,他们把fastjson1中的一些类库拆分了FastJsonHttpMessageConverter在2.0.X中不存在的问题 · Issue #168 · alibaba/fastjson2 (github.com)

 FastJSON1包

中我们用的是

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

FastJSON2包

升级到FastJSON2,我们则需要升级为

import com.alibaba.fastjson2.support.config.FastJsonConfig;import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;

解决方法 

pom/xml引入完整类库

所以你单单引入还不够,我们还需要引入拆分的 fastjson2-extensionfastjson2-extension-spring6

<!--  FastJson2中FastJsonHttpMessageConverter找不到类问题 by https://zhengkai.blog.csdn.net/-->
<!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
<dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>2.0.49</version>
</dependency>
<dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2-extension</artifactId><version>2.0.49</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2-extension-spring6 -->
<dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2-extension-spring6</artifactId><version>2.0.49</version>
</dependency>

WebMvcConfig和configureMessageConverters配置

 其中,记得把其他相关的也要升级一下JSON/JSONArray/JSONObject

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

(完整版供参考)

package com.softdev.system.generator.config;// import com.alibaba.fastjson.support.config.FastJsonConfig;
// import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import jakarta.servlet.DispatcherType;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
*  2019-2-11 liutf WebMvcConfig 整合 cors 和 SpringMvc MessageConverter
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");}@Beanpublic FilterRegistrationBean xssFilterRegistration() {FilterRegistrationBean registration = new FilterRegistrationBean();registration.setDispatcherTypes(DispatcherType.REQUEST);registration.setFilter(new XssFilter());registration.addUrlPatterns("/*");registration.setName("xssFilter");registration.setOrder(Integer.MAX_VALUE);return registration;}// @Override// public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {//     converters.clear();//     //FastJsonHttpMessageConverter//     FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();//     List<MediaType> fastMediaTypes = new ArrayList<>();//     fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);//     fastConverter.setSupportedMediaTypes(fastMediaTypes);//     FastJsonConfig fastJsonConfig = new FastJsonConfig();//     fastJsonConfig.setCharset(StandardCharsets.UTF_8);//     fastConverter.setFastJsonConfig(fastJsonConfig);//     //StringHttpMessageConverter//     StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();//     stringConverter.setDefaultCharset(StandardCharsets.UTF_8);//     stringConverter.setSupportedMediaTypes(fastMediaTypes);//     converters.add(stringConverter);//     converters.add(fastConverter);// }/*** FASTJSON2升级 by https://zhengkai.blog.csdn.net/* https://blog.csdn.net/moshowgame/article/details/138013669*/@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);}}

 MVN build successfully

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

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

相关文章

STM32H743驱动SD卡(1)

本文内容参考&#xff1a; STM32——SDIO的学习&#xff08;驱动SD卡&#xff09;&#xff08;理论篇&#xff09;-CSDN博客 STM32个人笔记-SDIO接口-CSDN博客 STM32-(40)&#xff1a;SD卡与SDIO-CSDN博客 【STM32】使用SDIO进行SD卡读写&#xff08;一&#xff09;-初步认…

【Python图像处理篇】opencv中的去畸变

去畸变 opencv opencv-python光学畸变校准 使用pythonopencv进行图像的去畸变 使用pythonopencv进行图像的去畸变 关于OpenCV中的去畸变 为什么相机参数每次标定的结果都不一样&#xff08;原理分析&#xff09;

GO的安装和配置

第一部分&#xff1a;GO语言基础 第1章&#xff1a;GO语言的安装和配置 在开始GO语言的学习和开发之前&#xff0c;首先需要确保你的计算机上安装了GO环境。本章将详细介绍如何在不同操作系统上安装GO语言&#xff0c;并配置相应的开发环境。 1.1 GO语言的安装步骤 对于Lin…

使用python-can和cantools实现arxml报文解析、发送和接收的完整指南

文章目录 背景一、硬件支持二、环境准备1、python解释器安装2、python库安装 三、 收发案例四、 方法拓展1、canoe硬件调用2、回调函数介绍 结论 背景 在汽车行业中&#xff0c;CAN (Controller Area Network) 总线是用于车辆内部通信的关键技术。arxml文件是一种用于描述CAN消…

【数据结构】算法效率揭秘:时间与空间复杂度的较量

前言 在计算机科学中&#xff0c;时间复杂度和空间复杂度是衡量算法性能的两个重要指标。它们分别表示算法在执行过程中所需的时间和空间资源。了解这两个概念有助于我们评估和比较不同算法的优劣&#xff0c;从而选择更合适的算法解决问题~ 欢迎关注个人主页&#xff1a;逸狼 …

.github/workflows Actions为项目构建增加手动CI 构建按钮

在Github CI项目的时候&#xff0c; 一般是有push的时候才触发CI构建任务&#xff0c; 今天介绍一种通过 on workflow_dispatch 来增加手动CI构建按钮的方法。 CI构建任务代码示例 .github/workflows/ci.yml name: CIon:push:branches: [develop]pull_request:branches: [dev…

社区论坛小圈子小程序源码系统:自定义小程序管理社区圈子软件圈子系统系统开发-做社区圈子丨圈子论坛社区交友系统开源版小程序源码丨

简述 移动互联网的快速发展&#xff0c;微信小程序作为一种新型的应用形态&#xff0c;已经深入到人们的生活中。特别是对于社区论坛类应用&#xff0c;小程序版本可以更好地满足用户快速、便捷获取信息的需求。下面给大家分享一款社区论坛小圈子小程序源码系统。 在这个信息…

RTT设备驱动框架学习(CAN设备)

RTT设备框架属于组件和服务层&#xff0c;是基于RTT内核之上的上层软件。 设备框架是针对某一类外设&#xff0c;抽象出来的一套统一的操作方法及接入标准&#xff0c;可以屏蔽硬件差异&#xff0c;为应用层提供统一的操作方法。 RTT设备框架分为三层&#xff1a;设备驱动层、…

linux中如何挂载yum云仓库进行软件的安装

1.首先在根目录下建立文件&#xff0c;用来挂载镜像文件 [rootclient ~]# mkdir /rhel9 2.挂载镜像文件&#xff1a; [rootclient ~]# mount /dev/cdrom /rhel9 3.切换到 /etc/yum.repos.d 下的目录并查看 &#xff0c;创建 rhel9.repo文件&#xff0c;并编辑云仓库域名&am…

Leetcode 410 分割数组

题目信息 LeetoCode地址: . - 力扣&#xff08;LeetCode&#xff09; 题目理解 将一个数组切k刀&#xff0c;每一块子数组求和&#xff0c;共k1个数&#xff0c;这里面有一个最大的数Max。找一种切法&#xff0c;使这个Max最小。 暴力解法一定是会超时的&#xff0c;因为包…

对前端路由的理解

在前端技术早期&#xff0c;一个 url 对应一个页面&#xff0c;如果要从 A 页面切换到 B 页面&#xff0c;那么必然伴随着页面的刷新。这个体验并不好&#xff0c;不过在最初也是无奈之举——用户只有在刷新页面的情况下&#xff0c;才可以重新去请求数据。 后来&#xff0c;改…

npm环境搭建

npm是什么 npm是前端的包管理工具&#xff0c;类似于后端的maven。现在npm已经集成到nodeJs中&#xff0c;安装好nodeJs就可以安装好npm了。 npm初始配置 一般下载好nodeJs后要对npm进行一些初始化配置。 修改npm的镜像源 npm默认的镜像源是https://registry.npmjs.org/&a…

西瓜书学习——线性回归

文章目录 基本格式线性回归一元线性回归多元线性回归 基本格式 f ( x ) w 1 x 1 w 2 x 2 . . . w d x d b f(x) w_1x_1 w_2x_2 ... w_dx_d b f(x)w1​x1​w2​x2​...wd​xd​b 一般可以表达为&#xff1a; f ( x ) w T x b f(x) w^Tx b f(x)wTxb w 和 b 可以通…

【前端】用CSS实现div全屏铺满的方式

在网页设计和开发中&#xff0c;有时我们需要让一个div元素全屏铺满整个浏览器窗口&#xff0c;以实现更加吸引人的视觉效果或者更好地适配不同设备的屏幕大小。 最近遇到一个需求&#xff0c;需要将一个div自动铺满全屏&#xff0c;width会默认铺满&#xff0c;所以不用考虑&…

LeetCode刷题总结 | 图论2—深度优先搜索广度优先搜索较为复杂应用

深搜广搜的标准模版在图论1已经整理过了&#xff0c;也整理了几个标准的套模板的题目&#xff0c;这一小节整理一下较为复杂的DFS&BFS应用类问题。 417 太平洋大西洋水流问题&#xff08;medium&#xff09; 有一个 m n 的矩形岛屿&#xff0c;与 太平洋 和 大西洋 相邻…

opencv图像处理

1、图像膨胀腐蚀 图像的膨胀(dilation)和腐蚀(erosion)主要用来寻找图像中的极大区域和极小区域。 膨胀类似于“领域扩张”&#xff0c;将图像的高亮区域或白色部分进行扩张&#xff0c;其运行结果图比原图的高亮区域更大。 腐蚀类似于“领域被蚕食”&#xff0c;将图像中的…

ElasticSearch有账号密码时: kibana配置

上一篇文章我们介绍过ElasticSearch关闭账号密码的的方式&#xff1a; config/elasticsearch.yml文件中 xpack.security.enabled: false 当我们关闭 账号密码&#xff0c;kibana是可以直接访问ElasticSearch的。 真实项目中&#xff0c;我们是不允许数据库裸跑的&#xff0c;所…

【前端甜点】某视频网站的m4s视频/音频下载方案(20240420)

引言 Sample&#xff1a;aHR0cHM6Ly93d3cuYmlsaWJpbGkuY29tL3ZpZGVvL0JWMWZKNDExUTdWeA 我在很久以前写过一个小脚本&#xff0c;发XHR请求来获取某视频网站的m4s视频和音频&#xff1a; // 唯一要改变的就是url(url must be https)&#xff0c;a.download是文件名&#xff…

【多线程学习】深入探究阻塞队列与生产者消费者模型和线程池常见面试题

˃͈꒵˂͈꒱ write in front ꒰˃͈꒵˂͈꒱ ʕ̯•͡˔•̯᷅ʔ大家好&#xff0c;我是xiaoxie.希望你看完之后,有不足之处请多多谅解&#xff0c;让我们一起共同进步૮₍❀ᴗ͈ . ᴗ͈ აxiaoxieʕ̯•͡˔•̯᷅ʔ—CSDN博客 本文由xiaoxieʕ̯•͡˔•̯᷅ʔ 原创 CSDN 如…

基于CppHttpLib的Httpserver

1 背景 大多数嵌入式设备由于没有屏幕输出&#xff0c;只能通过Web页面来配置。这里利用CPPHttpLib来实现HttpServer。 2 HttpServer HttpServer是利用CPPHttpLib开源库实现的Http服务器CppHttpLib是基于C11的HTTP开源库&#xff0c;开源协议是MIT. CppHttpLib下载地址 2.1 …