因为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方法。使用该类作为消息转换器。测试正常。
替换回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);
}
}
}