Spring Boot Rest-如何接受多个标题

问题描述 投票:1回答:1

我正在使用Spring Boot V2.2.2.RELEASE并使用自定义标头进行API版本控制。我像这样开发了一个小型端点:

@GetMapping(value = "/student/header", headers = {"X-API-VERSION=2", "X-API-VERSION=1"})
public StudentV1 headerV2() {
    return new StudentV1("Bob Charlie");
} 

当我按下curl -X GET http://localhost:8080/student/header -H 'x-api-version: 1'时,我得到了错误。

{
    "timestamp": "2020-01-13T09:20:20.087+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/student/header"
}

如果我使用headers = {"X-API-VERSION=2"},那么它可以工作,但是如果我使用headers = {"X-API-VERSION=2", "X-API-VERSION=1"},则事情将停止工作。

@GetMapping(value = "/student/header", headers = {"X-API-VERSION=2"})
public StudentV1 headerV2() {
  return new StudentV1("Bob Charlie");
}
spring swagger spring-rest
1个回答
0
投票

使用headers = {"X-API-VERSION=2", "X-API-VERSION=1"},两个标头都必须存在。

尝试对每个标头使用一个映射,然后转发到您的服务展示。

@GetMapping(value = "/student/header", headers = {"X-API-VERSION=1"})
public StudentV1 headerV1() {
    return serviceImpl.headerV1();
}

@GetMapping(value = "/student/header", headers = {"X-API-VERSION=2"})
public StudentV1 headerV2() {
    return serviceImpl.headerV2();
}
© www.soinside.com 2019 - 2024. All rights reserved.