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

发布于 2022-09-06 22:05:08 字数 3894 浏览 42 评论 0

在使用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]

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千里故人稀 2022-09-13 22:05:08

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文