Header Spring 引导 REST API 版本控制的自定义 Header 方法

Custom Header Approach for Spring Boot REST API versioning

我希望使用自定义 Headers 通过 Spring Boot 和 Swagger 实施 REST API 版本控制。我经历了很多 link 如下所示。

https://dzone.com/articles/versioning-rest-api-with-spring-boot-and-swagger

有一点很清楚,我的每个 REST 端点都必须用 Custom Header headers = {"X-API-VERSION=v1"}.

进行注释

下面是我的示例 Rest 端点。

@ApiOperation(value = "Find Employees", nickname = "Find Employees")
@ApiResponses(value = { @ApiResponse(code = 200, message = "For Success as well as No Data found scenerio."),
        @ApiResponse(code = 500, message = "Internal Server Error") })
@GetMapping(value = "/employees", headers = {"X-API-VERSION=v1"})
public ResponseEntity<List<Employee>> findEmployees() {
    List<Employee> EmployeeList= employeeservice.findAllEmployees();
    return new ResponseEntity<List<Employee>>(EmployeeList,HttpStatus.OK);
}

Swagger 配置changes/updates

@Configuration
@EnableSwagger2
public class SwaggerConfig{
    @Bean
    public Docket api() {
        ParameterBuilder aParameterBuilder = new ParameterBuilder();
        aParameterBuilder.name("X-API-VERSION=v1").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
        List<Parameter> operationParameters = new ArrayList<>();
        operationParameters.add(aParameterBuilder.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))
                .apis(RequestHandlerSelectors.basePackage("com.example"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo())
                .useDefaultResponseMessages(false)
                .globalOperationParameters(operationParameters)
                .globalResponseMessage(
                        RequestMethod.GET,
                        newArrayList(new ResponseMessageBuilder().code(500).message("").build()));
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API Management")
                .description("Sample Description")
                .termsOfServiceUrl("https://www.test.com")
                .version("0.1")
                .build();
    }
}

根据这个 link: https://www.springboottutorial.com/spring-boot-versioning-for-rest-services,我必须传递值 X-API-VERSION=v1,但是当我传递那个 /employees/v1/error 端点时,它被调用了,什么都没有留下,也没有控制器被调用。如何解决这个问题?

这就是这种方法的工作原理。

@RestController
public class TestController {


    @RequestMapping(value = "/user")
    public String getUserDefault() {
        return "getUserDefault";
    }

    @RequestMapping(value = "/user", headers = {"X-API-VERSION=v1"})
    public String getUserV1() {
        return "getUserV1";
    }

    @RequestMapping(value = "/user", headers = {"X-API-VERSION=v2"})
    public String getUserV2() {
        return "getUserV2";
    }

}

此外,您可以使用 GetMapping 而不是 RequestMapping,让我们使用 curl 进行测试:

curl --location --request GET 'http://localhost:8080/user' --header 'X-API-VERSION: v2'

return 200 getUserV2

curl --location --request GET 'http://localhost:8080/user' --header 'X-API-VERSION: v1'

return 200 getUserV1

curl --location --request GET 'http://localhost:8080/user'

return 200 getUserDefault