linux

Linux环境下Swagger如何实现API版本管理

小樊
36
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
看了该问题的人还看了