Swagger2 更改 Swagger Ui 的基本路径

Posted

技术标签:

【中文标题】Swagger2 更改 Swagger Ui 的基本路径【英文标题】:Swagger2 Change Base Path for Swagger Ui 【发布时间】:2018-01-01 07:27:55 【问题描述】:

只需添加此 SwaggerConfig 文件并添加以下依赖项即可将 Swagger 2 设置到我的 SpringBoot 应用程序:

@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>

尽管我的控制器类看起来像这样:

@RestController
@RequestMapping("/motorcycles")
public class ProductController  

// GET method


...我仍然可以通过这样做来调用该控制器:

curl -X GET http://localhost:8080/motorcycles

我必须使用以下 URL 路径打开 Swagger-ui.html 文件:

http://localhost:8080/swagger-ui.html

如何让我的 Spring Boot 应用程序显示类似这样的内容(实际应用程序名称或控制器中指定的默认 RequestMapping - 如果控制器是应用程序中唯一的控制器):

http://localhost:8080/motorcycles/swagger-ui.html 

基本上,如何在 swagger-ui.html 前加上应用名称?

所以,假设我的应用名为motorboy,这就是我想要的:

http://localhost:8080/motorboy/swagger-ui.html

REST Endpoint 的 Curl -X GET 如下所示:

http://localhost:8080/motorboy/motorcycles

似乎 spring-boot 只是使用普通的旧 http://localhost:8080 作为浏览器中的默认应用程序名称以进行招摇。

【问题讨论】:

如果你只是想为你的 Spring Boot 应用程序设置一个上下文路径,可以看看这个问题的答案***.com/a/20418450/5871191@RequestMapping("/motorcycles") 仅适用于 ProductController。 【参考方案1】:

我在application.properties 中解决了这个设置spring boot 应用程序上下文路径的问题(你可以用不同的方式设置这个变量,见this spring doc):

server.servlet.context-path=/user(在你的情况下是摩托男孩)

设置上下文路径后,我可以分别从 http://localhost:8080/user/swagger-ui.htmlhttp://localhost:8080/user/v2/api-docs 访问 swagger-ui 或 swagger 文档。

如果你愿意,我可以做一个简单的项目并更新到 github,只是为了澄清和解释这个配置。

【讨论】:

【参考方案2】:

这是一个解决方案/解决方法,有助于定义 swagger 的基本路径。

@Component
@Primary
public class CustomBasePathJsonSerializer extends JsonSerializer 

    private static final String BASE_PATH = "/my-base-path";

    public CustomBasePathJsonSerializer(List<JacksonModuleRegistrar> modules) 
        super(modules);
    

    @Override
    public Json toJson(Object toSerialize) 
        if (toSerialize instanceof Swagger) 
            Swagger swagger = (Swagger) toSerialize;
            swagger.basePath(BASE_PATH);
        
        return super.toJson(toSerialize);
    

【讨论】:

以上是关于Swagger2 更改 Swagger Ui 的基本路径的主要内容,如果未能解决你的问题,请参考以下文章

SpringFox swagger2 and SpringFox swagger2 UI 接口文档生成与查看

SpringBoot中部署Swagger2和Swagger-UI

Swagger2 UI 提示"请确保swagger资源接口正确"解决办法

springboot集成swagger2或swagger3

13.9 SpringBoot集成Swagger2中遇到的问题

swagger2注解使用教程