因为Fast作为JSON的序列化与反序列化一些优点,在项目中使用了FastJson库的FastJsonHttpMessageConverter4 作为Spring的消息转换器,可替换后,发现http://localhost:8081/swagger-ui.html页面能打开,但API内容都不见了。但v2/api-docs倒是能打开。
替换回Spring默认的Jackson2,页面打开正常。经过抓包,发现使用不同的转换器时,页面打开请求的URL是不同的。使用FastJson时,到swagger-resources/configuration/ui请求后,就结束了。而默认的Jackson2在此之后,又多了几个请求。对比swagger-resources/configuration/ui请求的响应数据,发现Jacson2比FastJson多了几个JSON数据项。
再返回系统中,查看日志。Debug跟踪发现MessageConverter的writeInternal方法内,写入的是UiConfiguration对象。而该类的一些属性使用了一些Jackson2的注解。正是这些注解的属性在FastJson中没有输出出来。这个类的输出,发现使用Jackson2来进行。
于是新写一个类,继承FastJsonHttpMessageConverter4 ,覆写writeInternal方法。使用该类作为消息转换器。测试正常。
public class SwaggerFastJsonHttpMessageConverter4 extends FastJsonHttpMessageConverter4 {
private ObjectMapper mapper = new ObjectMapper();
@Override
protected void writeInternal(Object obj, //
Type type, //
HttpOutputMessage outputMessage //
) throws IOException, HttpMessageNotWritableException {
if (type == springfox.documentation.swagger.web.UiConfiguration.class) {
HttpHeaders headers = outputMessage.getHeaders();
ByteArrayOutputStream outnew = new ByteArrayOutputStream();
mapper.writeValue(outnew, obj);
outnew.flush();
headers.setContentLength(outnew.size());
OutputStream out = outputMessage.getBody();
outnew.writeTo(out);
outnew.close();
} else {
super.writeInternal(obj, type, outputMessage);
}
}
}
分享到:
相关推荐
springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot ...
springboot2.0整合fastjson实例配置,在测试类中有各种fastjson的使用,序列化反序列化,以及jsonObject与jsonarray的使用,具体测试方法在测试类中
SpringMVC+Fastjson+Swagger集成示例完整源码Example。
实现json转换,fastjson json json转换,可以帮助开发中涉及json格式处理的程序猿。
springboot完美整合shiro、ehcache、redis、spring data jpa,swagger、fastjson,后续继续补充,适合初中级开发练习掌握框架,代码下载可直接运行,测试已通过
转换器FastJSON
Java编程中有时候会遇到JavaBean对象、json字符串、json对象之间互相转换的问题,个人感觉使用fastjson很方便,把jsonfast包和使用方法分享给大家.
fastjson jar 使用详解 实例 fastjson jar 使用详解 实例
比较好用的两个json生产工具,并且一直再用效率高依赖少。
简单的fastjson使用,包括序列化和反序列化
关于fastjson的完整使用案例,完整代码和注释,jar包,让你快速掌握fastjson的妙处,从此摆脱解析数据的烦恼。
fastjson使用详解
使用阿里巴巴提供的fastjson可以快速的实现java对象和json串之间的相互转换,避免字符串拼接的繁琐!
fastjson使用详解.txt
使用fastjson.jar解析复杂的json数据,包含完整实例代码
Gson和Fastjson 的简单使用
java使用fastJson处理复杂Json字符串,直接获取key对应的value值
https://blog.csdn.net/hykwhjc/article/details/81121224 fastJSON的使用
通过fastjson实现了JavaBean,list,MAP,list等格式与json格式之间的转换,已经附带fastjson的包,包的版本为fastjson-1.1.36.jar