使用SpringMVC和JDK 1.6在swagger api-docs上找不到404

时间:2018-08-28 11:32:31

标签: spring swagger-2.0 springfox

我正在开发一个使用jdk 1.6,SpringMVC但不使用SpringBoot的应用程序,而且我很难使工作迅速。 确实,我的控制台在对我说,似乎已经准备好要招摇了,如该日志所示:

13:09:44:213 CEST] 000000f8 SimpleUrlHand I org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler Mapped URL path [/swagger-ui.html] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
[28/08/18 13:09:44:264 CEST] 000000f8 SimpleUrlHand I org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler Mapped URL path [/webjars/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#1'
[28/08/18 13:09:44:792 CEST] 000000f8 RequestMappin I org.springframework.web.servlet.handler.AbstractHandlerMethodMapping registerHandlerMethod Mapped "{[/v2/api-docs],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String)
[28/08/18 13:09:44:798 CEST] 000000f8 RequestMappin I org.springframework.web.servlet.handler.AbstractHandlerMethodMapping registerHandlerMethod Mapped "{[/swagger-resources],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto org.springframework.http.ResponseEntity<java.util.List<springfox.documentation.swagger.web.SwaggerResource>> springfox.documentation.swagger.web.ApiResourceController.swaggerResources()

但是当我尝试使用此URL(X / Y是我的上下文根)时:

http://localhost:9080/X/Y/v2/api-docs

http://localhost:9080/X/Y/swagger-ui.html

Firefox向我发送了一个404找不到页面。

这是我的配置:

pom.xml:

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.0.1</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.0.1</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.0</version>
</dependency>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>X</display-name>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>


    <!-- Spring WS configurations -->
    <listener>
        <listener-class>com.bnpp.wallet.listener.JoranConfiguratorListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-ws-servlet.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
        <init-param>
          <param-name>com.sun.jersey.config.property.packages</param-name>
          <param-value>com.foo.bar.ws</param-value>
        </init-param>
        <init-param>
          <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
          <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <!-- IPS Compliance -->
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/WEB-INF/jsp/errorT.jsp</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/jsp/errorT.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/jsp/errorT.jsp</location>
    </error-page>
</web-app>

spring-ws-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:oxm="http://www.springframework.org/schema/oxm" 
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:sws="http://www.springframework.org/schema/web-services"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd">

    <!-- Scan d'un package et ses sous-packages -->

    <context:component-scan base-package="com.foo.bar" />
    <bean class="com.foo.bar.config.SwaggerConfig"/>
    <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
    <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/> 

    <mvc:annotation-driven />


    <!-- Génération dynamique du WSDL. -->
    <sws:dynamic-wsdl id="foo1" portTypeName="foo2"
        locationUri="/foo3/">
        <sws:xsd location="classpath:/xsd/foo4.xsd" />
    </sws:dynamic-wsdl>
</beans>

SwaggerConfig.java:

package com.foo.bar.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()
            .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("TITLE")
            .description("DESCRIPTION")
            .version("VERSION")
            .termsOfServiceUrl("http://terms-of-services.url")
            .license("LICENSE")
            .licenseUrl("http://url-to-license.com")
            .build();
    }
}

有人知道我为什么会有这个问题吗?我宁愿不要更改pom中的依赖关系,因为该应用程序非常复杂,除了工作量很大之外,其他工作方式都一样。如果这是pom问题,请告诉我。

当您看到foo和bar时,那就是隐私。

谢谢。

1 个答案:

答案 0 :(得分:0)

Swagger v2仅在JDK 1.7或更高版本中受支持。但是在某些情况下,您可以尝试添加依赖项:

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>18.0</version>
</dependency>

,这是Swagger v2运行所必需的。您可以看看您的依赖项。可能应该有Guava 15.0版。 这还取决于您所使用的应用服务器,因为它的工作原理不同。