swagger2(v:2.7)使用fastjson(v:1.2.40)转换消息时失效如何解决?

在使用swagger2生成api中,访问http://localhost:8080/swagger-ui.html的时候页面显示:

fetching resource list: http://localhost:8080/v2/api-docs; Please wait.

访问http://localhost:8080/v2/api-docs的时候返回空的json字符串{}.看介绍说fastjson在1.2.15的时候就已经解决了这个问题,但是我的版本是1.2.40,按理说没有问题,但是无奈却总是不对,但是我使用jackson就没有问题,访问http://localhost:8080/swagger-ui.html能正常显示文档.
网上对应的介绍地址:解决SpringMVC使用FastJsonHttpMessageConverter时Swagger2失效的办法

我的springMvc主要配置文件如下:

    <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
    <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
    
    <mvc:annotation-driven>
        <mvc:message-converters><!-- register-defaults="true"-->
            <!--将StringHttpMessageConverter的默认编码设为UTF-8-->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8"/>
            </bean>
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
                <property name="fastJsonConfig" ref="fastJsonConfig"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <bean id="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig">
        <property name="charset" value="UTF-8" />
        <property name="serializerFeatures">
            <list>
                <value>QuoteFieldNames</value>
                <value>WriteMapNullValue</value>
            </list>
        </property>
    </bean>

如上,使用FastJsonHttpMessageConverter就不能正常的访问,到使用jackson的converter时就可以,配置如下:

    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html; charset=UTF-8</value>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
    </bean>

而且访问swagger-ui.html控制台还总是提示Did not find handler method for

2017-12-28 13:45:50 DEBUG springfox.documentation.spring.web.PropertySourcedRequestMappingHandlerMapping.getHandlerInternal:310 - Looking up handler method for path /swagger-resources/configuration/ui
2017-12-28 13:45:50 DEBUG springfox.documentation.spring.web.PropertySourcedRequestMappingHandlerMapping.lookupHandlerMethod:108 - looking up handler for path: /swagger-resources/configuration/ui
2017-12-28 13:45:50 DEBUG springfox.documentation.spring.web.PropertySourcedRequestMappingHandlerMapping.getHandlerInternal:320 - Did not find handler method for [/swagger-resources/configuration/ui]

不知道该如何解决,求助.谢谢!

阅读 9.6k
2 个回答

我找一早上都没找到为什么,刚看到这里,我把fastjson的改成jackson的就可以了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进