我的项目中默认是这样使用FastJsonHttpMessageConverter的:

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
// 防止循环引用
SerializerFeature.DisableCircularReferenceDetect,
// 空集合返回[],不返回null
SerializerFeature.WriteNullListAsEmpty,
// 空字符串返回"",不返回null
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteMapNullValue
);
fastJsonConfig.getSerializeConfig().put(String.class,MyStringSerializer.instance);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
//处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON);
fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
converters.add(0, fastJsonHttpMessageConverter);//放到前面 }

之前本来相安无视,后面因为springboot升级到2.6.x,原有的springfox因为很久没有更新出现兼容问题(虽然可以解决),再加上想尝试使用新的openapi3,因此换用了最近有在更新的springdoc-openapi。结果两者配合使用时,swagger界面无法加载,而v3/api-docs中的json都多出了\:

"{\"openapi\":\"3.0.1\",\"info\":{\"title\":\"OpenAPI definition\",\"version\":\"v0\"},\"servers\":[{\"url\":\"http://localhost:8099\",\"description\":\"Generated server url\"}],\"paths\":{},\"components\":{}}"

这直接导致swagger无法正确的识别,从而界面加载失败。而原来使用springfox时,并没有出现这个问题。

经过打断点分析,发现原来使用springfox时,传到com.alibaba.fastjson.serializer.SerializeConfig#getObjectWriter(java.lang.Class<?>, boolean) 这里时,class是springfox.documentation.spring.web.json.Json,fastjson对此作了特殊处理,因此可以保持原样。而改换springdoc时,此处传入的class却是String! 而fastjson默认会对String进行处理,加上转义符号,因此导致最终的结果出现异常。

解决办法:

1、换成Jackson的converter(springboot默认就是Jackson)。

2、FastJsonHttpMessageConverter:自定义一个StringSerializer,覆盖掉fastjson的默认的StringSerializer

public class MyStringSerializer implements ObjectSerializer {
public static final MyStringSerializer instance = new MyStringSerializer();
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
SerializeWriter out = serializer.getWriter();
out.write(object.toString());
}
}

然后在FastJsonHttpMessageConverter的配置文件中,如下使用

fastJsonConfig.getSerializeConfig().put(String.class,MyStringSerializer.instance);

即可覆盖掉fastjson默认的StringSerializer。(也可以通过这么操作,对其它类进行自定义处理)。

因为理解不是很透彻,不清楚fastjson为什么默认要对String进行字符转义处理,也不太清楚这样修改是否会导致其他后果,如果不妥希望能指出。

(本bug已经提交到fastjson在github上的issues中,期望能得到解答,不过这个好像只能靠分析String里的内容来solve了...)

这个bug只有FastJsonHttpMessageConverter添加到converters最前面或在默认的Jackson之前时会引起这个问题,关于converters的顺序等更深入的探讨,可以参考这篇文章:

https://segmentfault.com/a/1190000012659486

SpringDoc-OpenApi与Fastjson冲突——FastJsonHttpMessageConverter对String的默认处理的更多相关文章

  1. fastjson转换包含date类型属性的对象时报错com.alibaba.fastjson.JSONException: For input string: "13:02:19"

    问题:time类型数据插入不进mysql数据库:调试的时候报如下错误: Caused by: java.lang.NumberFormatException: For input string: &q ...

  2. 将fastjson元素转化为String[]

    在fastjson中如果JSONObject中添加了 String[] 类型的元素 例如 JSONObject jo = new JSONObject(); String[] array = {&qu ...

  3. 使用FastJSON 对Map/JSON/String 进行互转

    Fastjson是一个Java语言编写的高性能功能完善的JSON库,由阿里巴巴公司团队开发的主要特性主要体现在以下几个方面: 1.高性能 fastjson采用独创的算法,将parse的速度提升到极致, ...

  4. com.alibaba.fastjson.JSONException: For input string: "8200-12-31"

    https://www.cnblogs.com/mengjinluohua/p/5544987.html https://samebug.io/exceptions/458113/com.alibab ...

  5. 您好,前端使用https,后端使用https是会有冲突的情况,所以默认后端都是http 负载均衡即可管理证书,不需要在后端ECS上绑定证书。

    您前端使用https,那么前端就是加密的,后端使用https就是会访问出现问题的,目前阿里云负载均衡默认的配置前端使用https,后端默认就是http,也是无法更改的. 前端使用https,目前只有一 ...

  6. SpringBoot更改HttpMessageConverters使用FastJson出现乱码问题

    1.出现问题的现象!如下截图,使用SpringBoot 进行开发,接口返回的内容出现中文乱码? 接口内容想要返回的内容: 页面返回内容: 惊喜不?意外不? 为什么出现这个情况?不例外的话,很多同事都是 ...

  7. Spring Boot返回json数据及完美使用FastJson解析Json数据

     Spring Boot返回json数据 视频地址:http://www.iqiyi.com/w_19rubxzsr5.html 博文参考:https://blog.csdn.net/linxingl ...

  8. springboot使用fastJson作为json解析框架

    springboot使用fastJson作为json解析框架 springboot默认自带json解析框架,默认使用jackson,如果使用fastjson,可以按照下列方式配置使用 〇.搭建spri ...

  9. FastJson测试用例

    基础测试 package com.ai; import com.ai.test.daily.Student; import com.alibaba.fastjson.JSON; import com. ...

随机推荐

  1. 读源码【读mybatis的源码的思路】

    ✿ 需要掌握的编译器知识 ★ 编译器为eclipse为例子 调试准备工作(步骤:Window -> Show View ->...): □ 打开调试断点Breakpoint: □ 打开变量 ...

  2. Elasticsearch笔记2

    1 搜索所有文档中还有某个字段的方法: localhost:9200/get-together/group/_search?pretty { "query": { "qu ...

  3. R数据分析:数据清洗的思路和核心函数介绍

    好多同学把统计和数据清洗搞混,直接把原始数据发给我,做个统计吧,这个时候其实很大的工作量是在数据清洗和处理上,如果数据很杂乱,清洗起来是很费工夫的,反而清洗好的数据做统计分析常常就是一行代码的事情. ...

  4. SpringBoot 自定义参数类型转换convert

    创建一个配置类.使用 @bean注入到容器中 @Bean public WebMvcConfigurer webMvcConfigurer(){ /** * 实现自定义的addConverter */ ...

  5. 正确理解jmeter线程组之Ramp-Up

    Ramp-Up表示多少时间内启动线程,比如线程数100,Ramp-Up设置为10,表示10秒内启动100线程,不一定是每秒启动10个线程: 下面我们来做几个测试 线程组设置:100线程,Ramp-Up ...

  6. Vue 源码解读(8)—— 编译器 之 解析(上)

    特殊说明 由于文章篇幅限制,所以将 Vue 源码解读(8)-- 编译器 之 解析 拆成了上下两篇,所以在阅读本篇文章时请同时打开 Vue 源码解读(8)-- 编译器 之 解析(下)一起阅读. 前言 V ...

  7. 3、如何查看window 下的cpu参数

    一.windows下查看CPU信息 windows系统使用systeminfo命令可以查看CPU信息,如: Intel64 Family 6 Model 158 Stepping 10 Genuine ...

  8. Linux下忘记MySQL密码的解决办法

    一.使用免密码登录 1.使用 #find / -name my.cfg 找到mysql配置文件 2.vim /etc/mysql/my.cfg (我的配置文件是这个路径,每个人的路径可能有所不同,用f ...

  9. Mac下的平铺式桌面 - Yabai

    Mac下的平铺式桌面 - Yabai 近来无事,凑着周末休息的时间,想折腾一下 Mac.很久之前就有朋友给我推荐过一款名为"Yabai"的平铺式桌面管理软件,今天,就折腾起来了. ...

  10. Pandas:to_excel时如何不覆盖之前的Excel表、ExcelWriter类

    如果只是想把一个DataFrame保存为单独的一个Excel文件,那么直接写: data.to_excel('xxx.excel','sheet1',index=False) 但是这样做,只会保存为单 ...