Swagger2 更改 Swagger Ui 的基本路径

2022-01-22 00:00:00 swagger java spring-boot maven swagger-2.0

只需添加此 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.htmlhttp://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.

相关文章