我们正在考虑在我们的 spring boot 应用程序中使用 header 字段来指定 REST API 版本。
我们如何告诉 spring boot 根据 header 值重定向调用?
我梦到的是这样的东西:
@Path("/my/rest/path")
@HeaderMapping(headerName="ApiVersion", headerValue="V1")
public class V1Controller {
@GetMapping
public String myMethod() {
}
}
== and ==
@Path("/my/rest/path")
@HeaderMapping(headerName="ApiVersion", headerValue="V2")
public class V2Controller {
@GetMapping
public String myMethod() {
}
}
对于这样的 HTTP 请求:
GET /my/rest/path HTTP/1.1
Accept: application/json
ApiVersion: V1
== or ==
GET /my/rest/path HTTP/1.1
Accept: application/json
ApiVersion: V2
最佳答案
这似乎可行:
@Path("/my/rest/path")
public class V1Controller {
@GetMapping(headers = "ApiVersion=V1")
public String myMethod() {
}
}
== and ==
@Path("/my/rest/path")
public class V2Controller {
@GetMapping(headers = "ApiVersion=V2")
public String myMethod() {
}
}
PS:尚未测试,但见 a Spring boot tutorial .
关于java - 如何根据 HTTP header 值在 RestController 之间切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51041204/