FastJson2中FastJsonHttpMessageConverter找不到类问题

news/2024/9/23 11:20:50/

 问题描述 

如果你最近也在升级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/news/1429551.html

相关文章

【软考】UML中的图之类图

目录 1. 说明2. 图示3. 类图使用方式3.1 对系统的词汇建模3.2 对简单的协作建模3.3 对逻辑数据库模式建模 1. 说明 1.类图&#xff08;Class Diagram&#xff09;展现了一组对象、接口、协作和它们之间的关系。2.在面向对象系统的建模中所建立的最常见的图是类图。3.类图给出系…

React - 高级用法

React高级用法 Hooks Reducer useReducer&#xff1a; import React, { useReducer, useState } from react;const initialState { count: 0 };function reducer(state, action) {switch (action.type) {case increment:return { count: state.count 1 }case decrement:re…

LabVIEW多设备控制与数据采集系统

LabVIEW多设备控制与数据采集系统 随着科技的进步&#xff0c;自动化测试与控制系统在工业、科研等领域的应用越来越广泛。开发了一种基于LabVIEW平台开发的多设备控制与数据采集系统&#xff0c;旨在解决多设备手动设置复杂、多路数据显示不直观、数据存储不便等问题。通过RS…

错过100w!

非纪实小说。大家就当看个热闹&#xff01; 他叫阿来&#xff0c;江苏人&#xff0c;西电研究生毕业&#xff0c;跟我来了同一家公司。 研究生薪资6000&#xff0c;本科生4200&#xff0c;他负责游戏开发&#xff0c;主要是做三方游戏框架移植。 平时只是打个照面&#xff0c;寒…

资料合集:超融合虚拟化与虚拟机管理特性解读

文章包含 DRS、GPU 直通与 vGPU 支持、快照、内容库、巡检中心等 SmartX 超融合虚拟化和虚拟机管理功能特性解读文章与短视频。“3 分钟特性解读”视频合集。 在评估超融合系统时&#xff0c;不少用户会关注计算虚拟化服务和围绕虚拟机生命周期管理的资源优化和运维管理功能。…

ActiveMQ 如果数据处理出现异常会怎么样

我们有一个 Spring 的客户端&#xff0c;在处理消息的时候因为程序的原因出现消息处理异常。 对这种情况&#xff0c;ActiveMQ 会把出现异常的消息放在 DLQ 队列中进行持久化。 因此&#xff0c;在 ActiveMQ 消息处理队列中需要持续关注 DLQ 队列&#xff0c; DLQ 的队列都是无…

润石科技(RUNIC)汽车电子应用方案和物料选型

一、润石科技&#xff08;RUNIC&#xff09;简介 江苏润石科技有限公司是一家专注于高性能、高品质模拟/混合信号集成电路研发和销售的高科技半导体设计公司。公司主要产品线分为两类&#xff1a;信号链和电源管理&#xff0c;其中信号链包含运算放大器、比较器、模拟开关、数…

FPGA学习路线

主要方向分以下个方向&#xff1a; &#xff08;1&#xff09;接口通信类&#xff1a; 简单通信接口SPI/UART/IIC&#xff0c; 复杂一点的SDR SDRAM控制器设计、DDR3MIG的使用、PCIE控制器、Aurora、千兆以太网通信&#xff1b; &#xff08;2&#xff09;数字信号处理类&…