鍍金池/ 問答/Java  網(wǎng)絡(luò)安全/ swagger2(v:2.7)使用fastjson(v:1.2.40)轉(zhuǎn)換消息時

swagger2(v:2.7)使用fastjson(v:1.2.40)轉(zhuǎn)換消息時失效如何解決?

在使用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的時候就已經(jīng)解決了這個問題,但是我的版本是1.2.40,按理說沒有問題,但是無奈卻總是不對,但是我使用jackson就沒有問題,訪問http://localhost:8080/swagger-ui.html能正常顯示文檔.
網(wǎng)上對應(yīng)的介紹地址:解決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的默認(rèn)編碼設(shè)為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]

不知道該如何解決,求助.謝謝!

回答
編輯回答
編輯回答
默念

我找一早上都沒找到為什么,剛看到這里,我把fastjson的改成jackson的就可以了

2018年1月17日 18:36