带有Spring MVC的Springfox Swagger2 - 404错误

时间:2017-12-15 20:15:39

标签: spring spring-mvc tomcat swagger

我试图将Springfox Swagger2集成到现有的Spring 4 MVC应用程序中,并遇到渲染某些页面的问题。

以下页面返回404错误:

  • https://localhost:9002/swagger-ui.html
  • webjars下的任何内容

以下页面正常工作并返回JSON:

  • https://localhost:9002/swagger-resources/configuration/security
  • https://localhost:9002/swagger-resources/configuration/ui
  • https://localhost:9002/v2/api-docs

我已将以下内容添加到我的MVC配置中:

<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

转到https://localhost:9002/swagger-ui.html会显示404错误:

[15/Dec/2017:16:36:07 -0500] "GET /swagger-ui.html HTTP/1.1" 404 36691 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.91 Safari/537.36"

转到https://localhost:9002/webjars/springfox-swagger-ui/springfox.js会显示相同的内容:

[15/Dec/2017:15:10:08 -0500] "GET /webjars/springfox-swagger-ui/springfox.js HTTP/1.1" 404 36691 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.91 Safari/537.36"

的web.xml

<?xml version="1.0" encoding="iso-8859-1"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <display-name>storefront</display-name>

    <!--
     We have to explicitly clear the welcome file list.
     We don't need to serve a default or index page as we can handle all the requests via spring MVC.
    -->
    <welcome-file-list>
        <welcome-file/>
    </welcome-file-list>

<context-param>
    <description>
    Spring Expression Language Support
    </description>
    <param-name>
    springJspExpressionSupport
    </param-name>
    <param-value>
    false
    </param-value>
</context-param>

    <!-- filters -->
<filter>
        <filter-name>UrlRewriteFilter</filter-name>
        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
        <!-- set the amount of seconds the conf file will be checked for reload
        can be a valid integer (0 denotes check every time,
        -1 denotes no reload check, default -1) -->
        <init-param>
            <param-name>confReloadCheckInterval</param-name>
            <param-value>600</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>UrlRewriteFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>



    <filter>
    <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <description>
            ResourceFilter
            Filter used to server file resources by bypassing the other filters.
        </description>
        <filter-name>resourceFilter</filter-name>
        <filter-class>com.company.storefront.servlets.ResourceFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>resourceFilter</filter-name>
        <url-pattern>/_ui/*</url-pattern>
        <url-pattern>/_public/*</url-pattern>
    </filter-mapping>

    <filter>
        <description>
            SSLTerminationFilter
            Allows for SSL termination at the loadbalancer/HTTP server level
        </description>
        <filter-name>SSLTerminationFilter</filter-name>
        <filter-class>com.acquitygroup.security.SSLTerminationFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>SSLTerminationFilter</filter-name>
        <servlet-name>DispatcherServlet</servlet-name>
        <dispatcher>REQUEST</dispatcher> 
    </filter-mapping>


    <!--  Begin custom security filters --> 

    <!--  Commented out for fixing log out issue

    <filter>
        <description>
            SecureSessionFilter
            Helps prevent session hi-jacking/fixation vulnerabilities
        </description>
        <filter-name>SecureSessionFilter</filter-name>
        <filter-class>com.acquitygroup.security.SecureSessionFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>SecureSessionFilter</filter-name>
        <servlet-name>DispatcherServlet</servlet-name>
    </filter-mapping>
     -->
    <filter>
       <filter-name>XSSFilter</filter-name>
       <filter-class>de.hybris.platform.servicelayer.web.XSSFilter</filter-class>
       <async-supported>true</async-supported>
    </filter>

    <filter-mapping>
       <filter-name>XSSFilter</filter-name>
       <url-pattern>/*</url-pattern>
       <dispatcher>REQUEST</dispatcher>
       <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

    <filter>
        <description>
            Spring configured based chain of the spring configurable filter beans
        </description>
        <filter-name>storefrontFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>storefrontFilterChain</filter-name>
        <servlet-name>DispatcherServlet</servlet-name>
    </filter-mapping>
    <filter>
        <description>
            OpenRedirectInterruptFilter
            Helps prevent against Open Redirect vulnerabilities
        </description>
        <filter-name>OpenRedirectInterruptFilter</filter-name>
        <filter-class>com.acquitygroup.security.OpenRedirectInterruptFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>OpenRedirectInterruptFilter</filter-name>
        <servlet-name>DispatcherServlet</servlet-name>
    </filter-mapping>

    <!--  End custom security filters -->   

    <!-- spring based filter chain -->


    <filter>
        <description>
            SpringSecurityFilterChain
            Supports delegating to a chain of spring configured filters. The filter name
            must match the bean name.
        </description>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <servlet-name>DispatcherServlet</servlet-name>
    </filter-mapping>



    <!--  punchout filter for IE 8 -->
    <filter>
        <filter-name>P3PFilter</filter-name>
        <filter-class>com.company.core.filter.P3PServletFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>P3PFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 


    <!-- HTTP Session Listeners -->
    <listener>
        <description>
            The HybrisContextLoaderListener extends the usual SpringContextLoaderListener (which loads
            the context from specified location) by adding the global application context of
            the platform as parent context. With having the global context set as parent you can access
            or override beans of the global context and get the 'tenant' scope.
        </description>
        <listener-class>de.hybris.platform.spring.HybrisContextLoaderListener</listener-class>
    </listener>

    <listener>
        <description>
            The RequestContextListener exposes the 'request' scope to the context.
            Furthermore it is needed when overriding the 'jalosession' bean for your web application.
        </description>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <listener>
        <description>
            The RequestContextListener exposes the 'request' scope to the context.
            Furthermore it is needed when overriding the 'jalosession' bean for your web application.
        </description>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <!-- config -->
      <context-param>
        <description>
            The 'contextConfigLocation' param specifies where your configuration files are located.
            The 'WEB-INF/config/web-application-config.xml' file includes several other XML config
            files to build up the configuration for the application.
        </description>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/config/web-application-config.xml</param-value>
    </context-param> 

    <!-- Servlets -->

    <!-- To Avoid server start up issues due to scope tenant  -->

       <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      
         <init-param>    
            <param-name>contextClass</param-name>
            <param-value>de.hybris.platform.spring.ctx.TenantIgnoreXmlWebApplicationContext</param-value>
        </init-param>

         <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/config/web-application-config.xml</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet> 
   <!--   <servlet>
        <description>
            DispatcherServlet
            Spring MVC dispatcher servlet. This is the entry point for the Spring MVC application.
        </description>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <description>
                Specifies the location for Spring MVC to load an additional XML configuration file.
                Because hybris is already configured with the XML spring configuration files to load
                we must set this param value to EMPTY in order to prevent loading of the default
                /WEB-INF/applicationContext.xml file.
            </description>

            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet> -->

    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <!-- Map all requests to the DispatcherServlet -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- Uncomment in to enable handling of 500 server errors  -->
    <error-page>
        <exception-type>java.lang.Exception</exception-type>
        <location>/WEB-INF/views/pages/error/serverError.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/views/pages/error/serverError.jsp</location>
    </error-page>

    <!-- handle csrf token errors due to session timeout! -->   
    <error-page>
        <exception-type>com.acquitygroup.security.csrf.RequestConfirmationTokenException</exception-type>
        <location>/login?timeout=true</location>
   </error-page>



    <!-- Session -->

    <session-config>
        <!-- Session timeout of 30 minutes -->
        <session-timeout>240</session-timeout>
    </session-config>


    <!-- JSP Configuration -->

    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <!-- Disable JSP scriptlets and expressions -->
            <scripting-invalid>true</scripting-invalid>
            <!-- Remove additional whitespace due to JSP directives -->
            <trim-directive-whitespaces>true</trim-directive-whitespaces>
        </jsp-property-group>
    </jsp-config>

</web-app>

只是寻找一些指导原因,以及如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

请检查aop版本,它应该是4x的相同版本。请参阅Integrating Swagger with Spring 3.0.6.RELEASEhttps://github.com/springfox/springfox/issues/938。这些是使用招摇的常见错误。