首页 > 解决方案 > swagger-ui.html 页面无法正常工作

问题描述

我正在将基于 springMVC 的项目迁移到 Spring Boot。这是一个多模块项目。我无法启动 swagger-ui.html。我添加了调度程序 servlet,如下所示:

@Configuration
public class DispatcherServletConfig {

@Bean
public DispatcherServlet dispatcherServlet() {
    return new DispatcherServlet();
}

@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
    ServletRegistrationBean registration = new 
  ServletRegistrationBean(dispatcherServlet(), "/prefix1/*");
    registration.addUrlMappings("/admin/*");
    registration.addUrlMappings("/prefix2/*");
     registration.setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME);
    return registration;
}

}
private static final Set<String> DEFAULT_PRODUCES_AND_CONSUMES = 
          new HashSet<String>(Arrays.asList("application/json",
              "application/xml"));

@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {

private static final Set<String> DEFAULT_PRODUCES_AND_CONSUMES = 
          new HashSet<String>(Arrays.asList("application/json",
              "application/xml"));

@Bean
public Docket api() {
    ParameterBuilder aParameterBuilder = new ParameterBuilder();
    aParameterBuilder.name("Authorization").modelRef(new ModelRef("string")).parameterType("header")
            .description("Authorization Header").required(true).build();
    List<Parameter> aParameters = new ArrayList<>();
    aParameters.add((Parameter) aParameterBuilder.build());
    return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any()).build().apiInfo(apiInfo()).globalOperationParameters(aParameters).produces(DEFAULT_PRODUCES_AND_CONSUMES);
}

private ApiInfo apiInfo() {
    Contact contact = new Contact("Power2SME Pvt. Ltd.", "http://www.power2sme.com", "support@power2sme.com");
    return new ApiInfo("NBFC API", "Information related to api exposed by NBFC system.", "1.0",
            "https://www.power2sme.com/termsandconditions", contact, "License of API",
            "https://www.power2sme.com/privacypolicy", new ArrayList<>());
}
}

我在 pom.xml 中添加了以下依赖项:

 <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-excelant</artifactId>
        <version>3.14</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>com.ibm.icu</groupId>
        <artifactId>icu4j</artifactId>
        <version>3.4.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.2.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.4</version>
    </dependency>
    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.0.6</version>
    </dependency>
    <dependency>
        <groupId>com.opencsv</groupId>
        <artifactId>opencsv</artifactId>
        <version>3.8</version>
    </dependency>
    <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.11</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>3.2-FINAL</version>
    </dependency>
    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>2.6.0</version>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-bundle</artifactId>
        <version>1.19.1</version>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.10.19</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.scribe</groupId>
        <artifactId>scribe</artifactId>
        <version>1.3.7</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.retrofit2</groupId>
        <artifactId>retrofit</artifactId>
        <version>2.4.0</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>logging-interceptor</artifactId>
        <version>3.10.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.8.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
        <version>2.9.6</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.retrofit2</groupId>
        <artifactId>retrofit</artifactId>
        <version>2.4.0</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>logging-interceptor</artifactId>
        <version>3.10.0</version>
    </dependency>
    <dependency>
        <groupId>com.squareup.retrofit2</groupId>
        <artifactId>converter-jackson</artifactId>
        <version>2.4.0</version>
    </dependency>
</dependencies>

还有一个插件:

<plugin>
            <groupId>io.github.robwin</groupId>
            <artifactId>swagger2markup-maven-plugin</artifactId>
            <version>0.9.3</version>
            <configuration>
                <inputDirectory>${project.basedir}/apiDoc/json</inputDirectory>
                <swaggerFile>swagger.json</swaggerFile>
                <outputDirectory>${project.basedir}/apiDoc/asciidoc</outputDirectory>
                <markupLanguage>asciidoc</markupLanguage>
            </configuration>
            <executions>
                <execution>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>process-swagger</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

我已将 apiDocs 存储在我的项目文件夹下。

每当我调用 {project_name}/prefix2/swagger-ui.html# 时,我都会收到 404 错误

在此处输入图像描述 每当我调用 {project_name}/swagger-ui.html# url 时,我都会得到这个 在此处输入图像描述

此外,swagger-ui.html 不会在 api 中加载。 在此处输入图像描述

我该如何解决这个问题?谢谢,

标签: javaspring-mvcspring-bootswagger-2.0

解决方案


对于使用较新版本的 swagger 的人,这里是访问 swagger ui 页面的解决方案。

替换这个:

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.8.0</version>
  </dependency>
    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.8.0</version>
    </dependency>

有了这个:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

在你的 pom.xml 文件中。

通过以下 url 访问页面(避免使用 .html 扩展名): http://localhost:8080/swagger-ui/

重建并重新启动服务器。你的问题就解决了!


推荐阅读