我的项目中默认是这样使用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. Elasticsearch使用系列-基本查询和聚合查询+sql插件

    Elasticsearch使用系列-ES简介和环境搭建 Elasticsearch使用系列-ES增删查改基本操作+ik分词 Elasticsearch使用系列-基本查询和聚合查询+sql插件 Elas ...

  2. js reduce累加器

    ​ reduce 是es6 新增的数组操作方法 意为累加器 使用方法如下 [1,1,1,1].reduce((total,currentValue,index)=>{ },initialValu ...

  3. PHP面试常考内容之面向对象(3)

    PHP面试专栏正式起更,每周一.三.五更新,提供最好最优质的PHP面试内容.继上一篇"PHP面试常考内容之面向对象(2)"发表后,今天更新面向对象的最后一篇(3).需要(1),(2 ...

  4. [LeetCode]14.最长公共前缀(Java)

    原题地址: longest-common-prefix 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入:st ...

  5. head 插件 Content-Type header [application/x-www-form-urlencoded] is not supported

    { "error": "Content-Type header [application/x-www-form-urlencoded] is not supported& ...

  6. spring boot全局配置文件优先级

    前两篇介绍的application配置文件,即为spring boot全局配置文件.那么spring boot加载配置文件的时候,怎么确定加载哪个目录下哪个文件呢? spring boot默认的配置文 ...

  7. JVM学习——类加载机制(学习过程)

    JVM--类加载机制 2020年02月07日14:49:19-开始学习JVM(Class Loader) 类加载机制 类加载器深入解析与阶段分解 在Java代码中,类型的加载.连接与初始化过程中都是在 ...

  8. html页面预览pdf文件使用插件pdfh5.js

    html预览pdf文件需要依赖pdf.js 移动端适配需要pdfh5.js 记录移动端适配pdfh5.js的用发 在线预览: https://www.gjtool.cn/pdfh5/pdf.html? ...

  9. 深入理解Java类加载机制,再也不用死记硬背了

    谈谈"会"的三个层次 在<说透分布式事务>中,我举例里说明了会与会的差别.对一门语言的学习,这里谈谈我理解的"会"的三个层次: 第一层:了解这门语言 ...

  10. python数据结构:链表

    链表与列表.数组这线性结构不同之处在于其在首末两端增删的话比较方便 单链表: 但是链表查找和删除的话都是需要从第一个开始从头查找 因此查找和删除的复杂度都为O(n) 双链表: 相比单链表来说,每个节点 ...