springmvc+swagger2整合
来源:互联网 发布:网站流量数据统计 编辑:程序博客网 时间:2024/06/05 05:10
整合的过程中遇到了一些问题,特此记录。
问题一:springmvc中使用fastJsonHttpMessageConverter导致swagger2 解析json失败若果换成mappingJackson2HttpMessageConverter就没事儿了,但是感觉fastjson性能更好一些,所以就得硬着头皮解决swagger2与fastjson兼容的问题。网上有很多解决方案,无非就是说换最高版本的fastjson就好了,但是我换了最新版的fastjson还是不行,不知道为什么,所以只能按照网上说的方法,来喽,大概意思就是自定义一个fastjson转换器。上代码
FastJsonHttpMessageConverterEx
package core.swagger;import springfox.documentation.spring.web.json.Json;import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;public class FastJsonHttpMessageConverterEx extends FastJsonHttpMessageConverter { public FastJsonHttpMessageConverterEx() { super(); this.getFastJsonConfig().getSerializeConfig().put(Json.class, SwaggerJsonSerializer.instance); }}SwaggerJsonSerializer
package core.swagger;import java.io.IOException;import java.lang.reflect.Type;import springfox.documentation.spring.web.json.Json;import com.alibaba.fastjson.parser.DefaultJSONParser;import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;import com.alibaba.fastjson.serializer.JSONSerializer;import com.alibaba.fastjson.serializer.ObjectSerializer;import com.alibaba.fastjson.serializer.SerializeWriter;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; }}springmvc配置文件
<mvc:annotation-driven><mvc:message-converters register-defaults="true"><bean id="fastJsonHttpMessageConverter" class="core.swagger.FastJsonHttpMessageConverterEx"><property name="supportedMediaTypes"><list><value>text/plain;charset=UTF-8</value><value>text/html;charset=UTF-8</value><value>application/json;charset=UTF-8</value></list></property><property name="features"><array value-type="com.alibaba.fastjson.serializer.SerializerFeature"><value>DisableCircularReferenceDetect</value><value>WriteMapNullValue</value><value>WriteNullNumberAsZero</value><value>WriteNullStringAsEmpty</value><value>WriteNullListAsEmpty</value><value>WriteNullBooleanAsFalse</value></array></property></bean></mvc:message-converters></mvc:annotation-driven>
成功解决问题
问题二:用springmvc写了一个登录拦截器,swagger2的所有请求都拦截了,由于swagger2是引入的jar包,所以跟了下代码,配置了一下拦截器
<mvc:interceptors><mvc:interceptor><mvc:mapping path="/**" /><mvc:exclude-mapping path="/webjars/**" /><mvc:exclude-mapping path="/swagger-resources/**" /><mvc:exclude-mapping path="/v2/**" /><bean class="interceptor.UserSecurityInterceptor"><property name="excludedUrls"><list><value>/tbuser/noUser</value><value>/tbuser/login</value><value>/swagger-ui.html</value></list></property></bean></mvc:interceptor></mvc:interceptors>成功解决问题
1 0
- springmvc+swagger2整合
- springmvc 整合 swagger2
- SpringMVC整合Swagger2
- SpringMVC整合swagger2
- SpringMVC中使用Swagger2整合
- swagger2 与 springmvc 整合 生成接口文档
- Spring MVC 整合 Swagger2
- Spring Boot整合Swagger2
- spring boot整合Swagger2
- SpringBoot整合Swagger2
- spring boot 整合Swagger2
- Swagger2+SpringMVC配置详解
- SpringMVC + Swagger2 的使用
- SpringMvc 集成swagger2
- SpringMVC集成swagger2
- 如何整合SpringMVC和Swagger2,并且使用Mock数据进行联调
- 个人推荐:SpringBoot整合Swagger2
- SpringMvc 3分钟集成swagger2
- MAC pro git 使用攻略
- SQL语句(增删改查)
- RNN的四种代表性扩展—Attention and Augmented Recurrent Neural Networks(二)
- getchar()的使用
- 什么是SOA
- springmvc+swagger2整合
- 一步步实现一个简单的下拉刷新上拉加载的通用框架
- 第十六周 项目一(7).归并排序
- android----使用SoundPool播放声音
- C Sharp 微信公众账号 Senparc.Weixin.MP SDK 开发教程 索引
- 第16周项目1-验证算法(4)基数排序
- 数据结构期末总结
- 资源地址 记录一下
- Elasticsearch与Solr 选型