Swagger2 Change Base Path for Swagger Ui(Swagger2 更改 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 Endpoint 的 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 的基本路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Swagger2 更改 Swagger Ui 的基本路径
基础教程推荐
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01