溫馨提示×

Linux環境下Swagger如何實現API版本管理

小樊
41
2025-09-08 06:17:54
欄目: 智能運維

在Linux環境下,使用Swagger實現API版本管理可以通過以下幾種方式:

1. 使用URL路徑進行版本管理

你可以在URL中包含版本號來區分不同的API版本。例如:

/api/v1/users
/api/v2/users

示例配置(Spring Boot + Swagger)

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket apiV1() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("v1")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller.v1"))
                .paths(PathSelectors.ant("/api/v1/**"))
                .build();
    }

    @Bean
    public Docket apiV2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("v2")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller.v2"))
                .paths(PathSelectors.ant("/api/v2/**"))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API Documentation")
                .description("API Documentation for multiple versions")
                .version("1.0.0")
                .build();
    }
}

2. 使用HTTP頭進行版本管理

你可以在HTTP請求頭中添加版本信息來區分不同的API版本。例如:

Accept: application/vnd.example.v1+json
Accept: application/vnd.example.v2+json

示例配置(Spring Boot + Swagger)

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket apiV1() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("v1")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller.v1"))
                .paths(PathSelectors.any())
                .build()
                .useDefaultResponseMessages(false);
    }

    @Bean
    public Docket apiV2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("v2")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller.v2"))
                .paths(PathSelectors.any())
                .build()
                .useDefaultResponseMessages(false);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API Documentation")
                .description("API Documentation for multiple versions")
                .version("1.0.0")
                .build();
    }
}

3. 使用媒體類型進行版本管理

你可以在HTTP請求的Content-TypeAccept頭中使用媒體類型來區分不同的API版本。例如:

Content-Type: application/vnd.example.v1+json
Accept: application/vnd.example.v1+json

示例配置(Spring Boot + Swagger)

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket apiV1() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("v1")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller.v1"))
                .paths(PathSelectors.any())
                .build()
                .produces(new String[]{"application/vnd.example.v1+json"});
    }

    @Bean
    public Docket apiV2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("v2")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller.v2"))
                .paths(PathSelectors.any())
                .build()
                .produces(new String[]{"application/vnd.example.v2+json"});
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API Documentation")
                .description("API Documentation for multiple versions")
                .version("1.0.0")
                .build();
    }
}

總結

以上三種方法都可以在Linux環境下使用Swagger實現API版本管理。選擇哪種方法取決于你的具體需求和項目架構。通常情況下,使用URL路徑進行版本管理是最直觀和常用的方法。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女