美文网首页
springmvc+swagger2整合

springmvc+swagger2整合

作者: TomHu_blog001 | 来源:发表于2019-08-18 08:48 被阅读0次

springmvc+swagger2整合

整合的过程中遇到了一些问题,特此记录。

问题一: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>

成功解决问题

相关文章

网友评论

      本文标题:springmvc+swagger2整合

      本文链接:https://www.haomeiwen.com/subject/egalsctx.html