FastJson2中FastJsonHttpMessageConverter找不到类问题

ops/2024/9/23 11:14:48/

 问题描述 

如果你最近也在升级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.ppmy.cn/ops/8224.html

相关文章

大华相机C#学习之Enumerator类

构造函数 Enumerator() 创建一个Enumerator实例。 常用方法 EnumerateDevices() 枚举所有发现设备,返回List<IDeviceInfo>对象。 List<IDeviceInfo> devices new List<IDeviceInfo>();private void test_Click(object sender, EventArgs e) {devicesEnum…

QT写Windows按键输出(外挂)

一、前言 玩游戏的时候遇到些枯燥无味反反复复的按鼠标键盘的情况时&#xff0c;就想写个外挂自动释放。刚好在学qt所以试验了下QT能不能对外输出按键与鼠标。 二、思路 qt中的按键鼠标全是输入&#xff0c;没有直接对外输出键盘鼠标指令的类&#xff0c;但是我们换个思路&…

SpringBoot:SpringMVC(下)

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、RequestBody补充二、PathVariable三.RequestPart:四.ResponseBody五.CookieValue&#xff0c;SessionAttribute&#xff0c;RequestHeader 前言 提示&…

JMeter--后置处理器--JSON提取器(JSON Extractor)

数据关联&#xff0c;可以通过JsonPath提取所需要的值&#xff0c;功能非常强大&#xff08;注意取样器返回必须为 Json&#xff09;&#xff1b;底层采用jackson实现&#xff1b; 右键 >>> 添加 >>> 后置处理器 >>> JSON提取器&#xff08;JSON E…

Java web第三次作业

springboot入门程序撰写并启动 2.使用postman练习参数的获取。 简单参数 复杂参数 数组参数 日期参数 json参数 路径参数 3、体会前端页面向后端发送数据的过程。并且自己尝试将之前的注册页面的信息发送到服务端。 &#xff08;1&#xff09;、product.html的操作代码&#…

Spring注解@ResponseBody的作用与应用场景

注解详情 ResponseBody 是 Spring MVC 中的一个注解&#xff0c;它的作用是将控制器中的方法返回值作为响应体&#xff08;Response Body&#xff09;直接返回给客户端&#xff0c;而不是作为视图模板&#xff08;View Template&#xff09;进行渲染。 在 Spring MVC 中&…

14. Spring AOP(二)实现原理

源码位置&#xff1a;spring_aop 上一篇文章中我们主要学习了AOP的思想和Spring AOP使用&#xff0c;本文讲的是Spring是如何实现AOP的&#xff0c;Spring AOP是基于动态代理来实现AOP的&#xff0c;在将动态代理之前先来了解一下什么是代理模式。 1. 代理模式 在现实中就有许…

nodejs常用命令

项目管理&#xff1a; $ npm init&#xff1a;初始化一个新的Node.js项目。 $ npm install &#xff1a;安装指定的Node.js包。 $ npm install &#xff1a;安装指定版本的Node.js包。 $ npm uninstall &#xff1a;卸载指定的Node.js包。 $ npm update&#xff1a;更新项目的…