SpringMVC中使用FastJsonHttpMessageConverter时Swagger2失效的解决办法
2017-06-26 14:08
471 查看
2016-08-01补充:该问题解决已合并入fastjson 1.2.15版本,请使用1.2.15+版本就不需要做下面的改造了
FastJson是阿里巴巴开源的高性能JSON转换工具。我们在使用Spring MVC需要进行JSON转换时,通常会使用FastJson提供的FastJsonHttpMessageConverter。但是在我们使用了Swagger2的工程中使用它之后,我们的Api文档就无法工作了(虽然swagger-ui界面可以展现,但是没有任何api内容,并且通过访问
通过下载FastJson源码之后,单步调试可以确定问题在于FastJson默认提供的
下面具体说说,如何来解决这个问题。
本文采用
通过查看
最后,在配置文件中用
FastJson是阿里巴巴开源的高性能JSON转换工具。我们在使用Spring MVC需要进行JSON转换时,通常会使用FastJson提供的FastJsonHttpMessageConverter。但是在我们使用了Swagger2的工程中使用它之后,我们的Api文档就无法工作了(虽然swagger-ui界面可以展现,但是没有任何api内容,并且通过访问
/v2/api-docse,我们得到的返回时
{},而不是之前的文档配置内容了。
通过下载FastJson源码之后,单步调试可以确定问题在于FastJson默认提供的
Serializer转换
springfox.documentation.spring.web.json.Json的结果为
{}。
下面具体说说,如何来解决这个问题。
更新fastjson版本
更新fastjson版本为1.2.10以上,在之前的版本中FastJsonHttpMessageConverter没有暴露fastjson对Serializer配置。更新到新版本之后,可以方便我们加入对
springfox.documentation.spring.web.json.Json的序列化支持。
本文采用
1.2.12为例:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.12</version> </dependency>
实现自定义序列化类
通过实现ObjectSerializer和
ObjectDeserializer接口,分别定义目标的序列化实现和反序列化实现。这里,我们只需要重写
write方法,实现序列化过程,将
springfox.documentation.spring.web.json.Json对象中的json文档正确返回即可。
public class Json { private final String value; public Json(String value) { this.value = value; } @JsonValue @JsonRawValue public String value() { return this.value; } }
通过查看
springfox.documentation.spring.web.json.Json源码,并单步调试即可知道swagger-ui所需要的返回内容就是Json对象中的value字段提供的。因此我们只需要在write方法中,将Json对象的value值输出即可,具体如下:
public class SwaggerJsonSerializer implements ObjectSerializer, ObjectDeserializer { public final static SwaggerJsonSerializer instance = new SwaggerJsonSerializer(); @Override public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException { SerializeWriter out = serializer.getWriter(); Json json = (Json) object; out.write(json.value()); } @Override public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { return null; } @Override public int getFastMatchToken() { return 0; } }
FastJsonHttpMessageConverter中加入自定义序列化类
实现FastJsonHttpMessageConverter的子类,并在构造函数中,加入springfox.documentation.spring.web.json.Json类与
SwaggerJsonSerializer的映射关系,使得在转换的时候,碰到
springfox.documentation.spring.web.json.Json就使用我们自己实现的
SwaggerJsonSerializer来进行转换,具体如下:
public class FastJsonHttpMessageConverterEx extends FastJsonHttpMessageConverter { public FastJsonHttpMessageConverterEx() { super(); this.getFastJsonConfig().getSerializeConfig().put(Json.class, SwaggerJsonSerializer.instance); } }
最后,在配置文件中用
FastJsonHttpMessageConverterEx替换原来的
FastJsonHttpMessageConverter即可。
相关文章推荐
- Springboot使用FastJsonHttpMessageConverter导致Swagger2失效
- springmvc项目使用fastjson作为json处理器时出现重复引用、循环引用的解决办法
- FastJsonHttpMessageConverter设置dateFormat后导致JSONField注解format失效解决办法
- JQuery中Ajax的Post提交中文乱码、windows.location.href 中文汉字乱码和springmvc使用原生态HttpServletRequest接收参数中文乱码解决办法
- SpringMvc项目 FastJson的数据中有$ref解决办法
- 使用fastjson与SpringMVC实现自定义HttpMessageConverter接收和获取JSON格式的数据
- SpringMVC 使用FastJSON 406错误解决
- SpringMVC使用FastJson并解决长数值精度丢失问题
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- 在C#单元测试中使用HttpContext的简单解决办法
- 另一应用程序已使用 HTTP.SYS 注册了该 URL。的解决办法。
- 使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法
- VC6.0“打开”和“添加到工程”失效,使用对应快捷键出错的解决办法
- “操作必须使用一个可更新的查询”的解决办法(转自:http://jingyan.baidu.com/article/d5a880eb46345613f147cc16.html)
- 在C#单元测试中使用HttpContext的简单解决办法
- SpringMVC使用@ResponseBody输出字符串时遇到的乱码问题及解决办法
- ASIHTTPRequest使用过程中遇到的问题及解决办法
- [转]HttpWebRequest使用注意(发生阻塞的解决办法)
- 使用RHTTPTransaction后产生CONE 36 Panic的解决办法
- ASIHTTPRequest使用过程中遇到的问题及解决办法