添加了 Springfox Swagger-UI 但它不起作用,我错过了什么?

pup*_*eno 23 spring swagger swagger-ui spring-boot springfox

按照此处的说明操作:

http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

我将这些依赖项添加到我的项目中:

compile "io.springfox:springfox-swagger2:2.7.0"
compile "io.springfox:springfox-swagger-ui:2.7.0"
Run Code Online (Sandbox Code Playgroud)

并像这样配置 SpringFox Swagger:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是 Swagger UI 似乎没有启用。我试过:

我得到的只是:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Sep 11 09:43:46 BST 2017
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
Run Code Online (Sandbox Code Playgroud)

在日志上我看到:

2017-09-11 09:54:31.020  WARN 15688 --- [nio-8080-exec-6] o.s.web.servlet.PageNotFound             : Request method 'GET' not supported
2017-09-11 09:54:31.020  WARN 15688 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
Run Code Online (Sandbox Code Playgroud)

http://localhost:8080/swagger-resources返回:

[{"name": "default",
  "location": "/v2/api-docs",
  "swaggerVersion": "2.0"}]
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

Mur*_*ade 28

我尝试了大多数这些答案,最终的解决方案是爬行..

正确的网址如下

http://localhost:8080/swagger-ui/

我正在使用 Springfox swagger-ui 3.xx

请参阅完整的招摇设置:http : //muralitechblog.com/swagger-rest-api-dcoumentation-for-spring-boot/

  • 最后的`/`是非常重要的部分 (13认同)
  • 天哪,这太令人难以置信了。我的整个周末都浪费在这上面了。感谢您提供这个解决方案。 (2认同)

Rav*_*ekh 21

io.springfox >= 2.X

io.springfox >= 3.X

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-schema</artifactId>
<version>2.9.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
浏览器网址
http://localhost:8080/swagger-ui.html
浏览器网址
http://localhost:8080/swagger-ui/#/
必须需要

mvn clean

必须需要

mvn clean

@Configuration
@EnableSwagger2
Run Code Online (Sandbox Code Playgroud)
@Configuration
@EnableSwagger2
Run Code Online (Sandbox Code Playgroud)

  • 这是关键:`mvn clean` (2认同)

viv*_*kar 20

已经有很多答案给出了正确的答案,但仍然存在一些关于错误的混淆。

如果你使用的是Spring Boot Version >= 2.2,建议使用SpringFox Swagger 3.0.0版

现在,只需要在 pom.xml 中添加一个依赖项。

<!-- Swagger dependency -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

应用程序启动后,您可以通过点击任一新的 swagger URL 来获取文档

选项 1:http://localhost:8080/swagger-ui/

选项 2:http://localhost:8080/swagger-ui/index.html

  • 谢谢,这有帮助 (2认同)
  • 非常感谢。这救了我的命。我输入的 URL 为“http://localhost:8080/swagger-ui”,但无法获取 UI,但末尾带有“/”,它呈现得就像魔术一样。你能解释一下最后这个“/”吗?是强制性的吗? (2认同)

Sta*_*avL 10

我遇到了这个问题,因为我的端点具有具有以下形式的路径变量的请求映射:/{var}。事实证明,这对于 GET 和 POST 端点都是一个问题,即 GET /{var} 和 POST /{var} 块 swagger-ui。一旦我使路径更加具体,我就可以使用 swagger-ui 来工作。

引自https://github.com/springfox/springfox/issues/1672

当 spring 找到一个只有一个变量 swagger 的简单路径时,无法拦截 URL。

通过调查评论中的各种想法发现。


Ous*_*ama 6

对于 Spring 版本 >= 2.2,你应该添加依赖 springfox-boot-starter

pom.xml:

<properties>
    <java.version>1.8</java.version>
    <io.springfox.version>3.0.0</io.springfox.version>
</properties>

<dependencies>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${io.springfox.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${io.springfox.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-data-rest</artifactId>
        <version>${io.springfox.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-bean-validators</artifactId>
        <version>${io.springfox.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>${io.springfox.version}</version>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

应用程序SwaggerConfig

@Configuration
@EnableSwagger2
public class ApplicationSwaggerConfig {

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

}
Run Code Online (Sandbox Code Playgroud)

Swagger-UI 链接: http://localhost:8080/swagger-ui/index.html#/