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.hqwc.cn/news/638392.html

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

相关文章

舒适护眼模式:苹果手机字体大小怎么设置?

在现代社会&#xff0c;人们对于手机的使用已经不再局限于通讯和娱乐&#xff0c;也逐渐成为了我们生活和工作中不可或缺的一部分。然而&#xff0c;长时间使用手机可能会对我们的视力造成一定程度的影响。 为了更好地保护视力健康&#xff0c;苹果手机提供了舒适护眼的模式&a…

【Linux】认识文件(二):重定向

【Linux】认识文件&#xff08;二&#xff09;&#xff1a;重定向 一.stdout,stderr,stdin二.重定向1.什么是重定向i.输出重定向>ii.追加重定向>>iii.输入重定向< 2.重定向原理3.dup2的使用 三.理解linux中的一切皆文件 上篇文件博客中&#xff0c;讲了进程管理已打…

见鬼了!我家的 WiFi 只有下雨天才能正常使用...

这是作者大学时期在家里遇到的一个非常奇怪的网络问题&#xff0c;作者的父亲是一名经验丰富的网络工程师&#xff0c;他们家里使用了一个复杂的网络设置&#xff0c;通过 Wi-Fi 桥接的方式&#xff0c;将父亲公司的高速商业网络连接到家中。但是有一天&#xff0c;作者发现家里…

互联网技术知识点总览——数据库知识点框架

简介 本文对数据库的知识点整体框架进行梳理和分享如下&#xff1a;

【图说】VMware Ubuntu22.04 详细安装教程

前言 无论是从事 Linux 开发工作&#xff0c;还是希望电脑运行双系统&#xff0c;VMware 虚拟机都是我们日常工作不可或缺的工具。本章将会重点介绍 VMware 安装流程&#xff0c;以及在 VMware 上如何运行、使用 Ubuntu22.04 系统。 一、VMware 下载安装 1.1 VMware 官网下载…

Bootstrap 5 保姆级教程(十五):表单

一、表单 1.1 堆叠表单 以下实例使用两个输入框&#xff0c;一个复选框&#xff0c;一个提交按钮来创建堆叠表单&#xff1a; <div class"container mt-3"><h2>堆叠表单</h2><form action""><div class"mb-3 mt-3"…

Oracle VM VirtualBox虚拟机安装配置

一、下载镜像文件 首先&#xff0c;需要从Oracle官方网站下载VirtualBox的最新版本。下载完成后&#xff0c;双击安装包开始安装。按照安装向导的提示&#xff0c;同意许可协议&#xff0c;并选择安装位置。在安装过程中&#xff0c;可以根据需要选择是否创建快捷方式、是否安…

150G全国1米分辨率土地利用数据【2023年】

#1数据摘要 全国1米分辨率土地利用数据 全国范围,分省份分类,1米精度土地利用数据。2023年版本。 数据格式:tif 坐标系:wgs1984 范围:全国各省份 时间:2023年 精度:1米 地类:共计11个地类 中国1m分辨率土地覆盖数据 文件命名与介绍:数据为GeoTIFF栅格格式,每个城市…

CK_Label_V1

CK_Label_v1&#xff08;电池版&#xff09; 产品型号 CK_Label_v1 尺寸 37*65*33.7mm 按键 1 指示灯 1 RGB灯(红/绿/蓝/黄/紫/白/青) 外观颜色 白色 供电方式 5号干电池供电1800mAh&#xff0c;可更换电池 通信方式 无线通信 合规认证 CE, RoHS 工作温度 …

Oracle交换分区测试

1、用exchange分区表减少初始化过程中对业务中断的影响 2、创建分区表 create table t_p (id number,name varchar2(30),addr varchar2(50)) partition by range(id) (partition p1 values less than(10), partition p2 values less than(20), partition p3 values less …

39. UE5 RPG角色释放技能时转向目标方向

在上一篇&#xff0c;我们实现了火球术可以向目标方向发射&#xff0c;并且还可以按住Shift选择方向进行攻击。技能的问题解决&#xff0c;现在人物释放技能时&#xff0c;无法朝向目标方向&#xff0c;接下来我们解决人物的问题。 实现思路&#xff1a; 我们将使用一个官方的…

【python】图形用户界面学习之tkinter

认识tkinter Tkinter是Python中内置的图形用户界面&#xff08;GUI&#xff09;库。它是Tk GUI工具包的接口&#xff0c;可以创建和管理窗口、按钮、标签、文本框等各种GUI组件&#xff0c;并与用户交互。 使用Tkinter&#xff0c;可以创建各种GUI应用程序&#xff0c;如桌面…