Swagger2更改Swagger Ui的基本路径 [英] Swagger2 Change Base Path for Swagger Ui
问题描述
只需添加此SwaggerConfig文件并添加以下依赖项,就可以将Swagger 2设置到我的SpringBoot应用程序中:
Was able to setup Swagger 2 to my SpringBoot app by just adding this SwaggerConfig file and adding the following dependencies:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(Predicates.not(PathSelectors.regex("/error"))).build().apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo("Motorcycle Shop - Restful Services", "Rest based API for Motorcycle Shop", "1.0", "",
new Contact("", "", ""), "", "");
return apiInfo;
}
}
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
尽管我的控制器类看起来像这样:
Despite fact that my controller class looks like this:
@RestController
@RequestMapping("/motorcycles")
public class ProductController {
// GET method
}
...仍然可以通过执行以下操作来调用该控制器:
... am still able to invoke that controller by doing this:
curl -X GET http://localhost:8080/motorcycles
我必须使用以下URL路径打开Swagger-ui.html文件:
I have to open up the Swagger-ui.html file using the following URL path:
http://localhost:8080/swagger-ui.html
如何使我的Spring Boot应用显示类似这样的内容(实际的应用名称或在控制器中指定的默认RequestMapping-如果该控制器是应用中唯一的控制器):
How can I make my Spring Boot app to show something like this (the actual app name or the default RequestMapping specified in a controller - if the controller is the only one in the app):
http://localhost:8080/motorcycles/swagger-ui.html
基本上,如何为swagger-ui.html加上应用名称?
Basically, how can I prefix the swagger-ui.html with the app name?
所以,可以说我的应用程序叫做motorboy,这就是我想要的:
So, lets say my app is called motorboy, this is what I want:
http://localhost:8080/motorboy/swagger-ui.html
REST端点的Curl -X GET看起来像这样:
And the Curl -X GET for the REST Endpoint looks like this:
http://localhost:8080/motorboy/motorcycles
似乎spring-boot只是使用普通的 http://localhost:8080 作为默认应用名称在浏览器中大摇大摆.
It seems that spring-boot is just using plain old http://localhost:8080 for the default app name in the browser for swagger.
推荐答案
我解决了这个问题,该问题在 application.properties
中设置了Spring Boot应用程序的上下文路径(您可以通过其他方式设置此变量,参见此春季文档):
I solved this problem setting context path to spring boot application in the application.properties
(you can set this variable with different ways, see this spring doc):
server.servlet.context-path =/user
(在您的情况下为Motorboy)
server.servlet.context-path=/user
(or motorboy in your case)
设置上下文路径后,我可以从 http://localhost:8080/user/swagger-ui.html
和 http://localhost:8080访问swagger-ui或swagger文档/user/v2/api-docs
.
After set context path I can access swagger-ui or swagger docs from http://localhost:8080/user/swagger-ui.html
and http://localhost:8080/user/v2/api-docs
respectively.
如果您愿意,我可以做一个简单的项目并更新到github,以阐明和解释此配置.
If you want I can make a simple project and update to github just for clarify and explain this configuration.
这篇关于Swagger2更改Swagger Ui的基本路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!