Swagger with Spring Boot 2.0 导致 404 错误页面 [英] Swagger with Spring Boot 2.0 leads to 404 error page
问题描述
我正在尝试将我的 Spring Boot 版本 2.0.1.RELEASE
与 Swagger.
I'm trying to integrate my Spring Boot version 2.0.1.RELEASE
with Swagger.
从这个博客文章看来是这样的只需添加两个 Maven 依赖项,一切都会很容易.
From this blog post it seemed like it will be easy by just adding two Maven dependencies and everything should work.
所以我在 pom 中添加了以下依赖项:
So I added the following dependencies to the pom:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
并创建了 SwaggerConfig
bean:
And created the SwaggerConfig
bean:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
return docket;
}
}
在属性文件中,我在尝试使其工作时得到了这 3 个条目:
And in the properties file I ended up with these 3 entries during the attempts to make it work:
spring.application.name=cat-service
management.server.servlet.context-path=/cat-service
server.servlet.contextPath=/cat-service
但最后,当访问
http://localhost:8080/cat-service/api/v2/api-docs
或
http://localhost:8080/cat-service/swagger-ui.html
我收到一个页面未找到
错误.
我在 swagger github 页面中发现了 这个问题 和 stackoverflow 中的这个问题 但我无法更改我的 404
错误.
I found this issues in the swagger github page and this question in stackoverflow but I was not able to change my 404
error.
推荐答案
我能够使其与 Spring Boot 版本 2.0.4.RELEASE
和 这篇博文:
I was able to make it work with Spring boot version 2.0.4.RELEASE
and this blog post:
我添加了这些依赖项:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
还有这个配置文件:
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 SpringFoxConfig {
@Bean
public Docket apiDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
它奏效了.
Swagger UI 可以通过/swagger-ui.html# 访问
The Swagger UI can be reached at /swagger-ui.html#
这篇关于Swagger with Spring Boot 2.0 导致 404 错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!