我的项目中默认是这样使用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. async同步异步

    1.同步:var async = require("async"); async.series([step1, step2, step3],function(err, values ...

  2. 微服务从代码到k8s部署应有尽有系列(三、鉴权)

    我们用一个系列来讲解从需求到上线.从代码到k8s部署.从日志到监控等各个方面的微服务完整实践. 整个项目使用了go-zero开发的微服务,基本包含了go-zero以及相关go-zero作者开发的一些中 ...

  3. ASP.NET Core 6框架揭秘-实例演示版[持续更新中…]

    作为<ASP.NET Core 3框架揭秘>的升级版,<ASP.NET Core 6框架揭秘>提供了很多新的章节,同时对现有的内容进行大量的修改.虽然本书旨在对ASP.NET ...

  4. jdk、jre、jvm分别是什么?有什么联系?

    JDK:是Java Development Kit的缩写,是Java的开发工具包,JDK是整个JAVA的核心.它提供了编译.运行Java程序所需的各种工具和资源.有了它,Java开发者就可以编译和运行 ...

  5. mysql5.7下载

    官网:https://dev.mysql.com/doc/refman/5.7/en/installing.html 二进制安装:https://dev.mysql.com/doc/refman/5. ...

  6. [杂记]CodeBlocks下载、安装及设置

    zyy安装codeblocks十次有九次都会卡在奇怪的地方,所以写篇博文以提醒自己少犯蠢[叹气] 下载 http://www.codeblocks.org/downloads/26 这是官网,以win ...

  7. 帆软和思迈特软件Smartbi产品的详细对比

    一.设计模式上 (1)finereport V10.0需要下载一个600M的设计器,采用类excel的设计模式,打开时间有20S,反复测试几次,基本都在20多秒.(测试电脑core I5 4核8G). ...

  8. 转载 CoreCLR源码探索(七) JIT的工作原理(入门篇)

    转载自:https://www.cnblogs.com/zkweb/p/7687737.html 很多C#的初学者都会有这么一个疑问, .Net程序代码是如何被机器加载执行的? 最简单的解答是, C# ...

  9. MSBuild 和项目文件

    Microsoft 生成引擎(MSBuild)项目文件位于生成和部署过程的核心. 本主题以 MSBuild 和项目文件的概念性概述开头. 它介绍了在处理项目文件时将遇到的关键组件,并通过一个示例来演示 ...

  10. 浅谈cache

    2021.9.22更新: <浅谈Cache Memory> http://blog.sina.com.cn/s/blog_6472c4cc0102dusv.html 为什么贴上这个链接呢, ...