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/
Rav*_*ekh 21
io.springfox >= 2.X |
io.springfox >= 3.X |
---|---|
Run Code Online (Sandbox Code Playgroud) |
Run Code Online (Sandbox Code Playgroud) |
浏览器网址 http://localhost:8080/swagger-ui.html |
浏览器网址 http://localhost:8080/swagger-ui/#/ |
必须需要
|
必须需要
|
Run Code Online (Sandbox Code Playgroud) |
Run Code Online (Sandbox Code Playgroud) |
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
Sta*_*avL 10
我遇到了这个问题,因为我的端点具有具有以下形式的路径变量的请求映射:/{var}。事实证明,这对于 GET 和 POST 端点都是一个问题,即 GET /{var} 和 POST /{var} 块 swagger-ui。一旦我使路径更加具体,我就可以使用 swagger-ui 来工作。
引自https://github.com/springfox/springfox/issues/1672
当 spring 找到一个只有一个变量 swagger 的简单路径时,无法拦截 URL。
通过调查评论中的各种想法发现。
对于 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#/
归档时间: |
|
查看次数: |
37415 次 |
最近记录: |