我们正在考虑使用标头字段在我们的Spring Boot应用程序中指定REST API版本。
我们如何告诉Spring Boot根据标头值重定向呼叫?
我梦到这样的事情:
@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
答案 0 :(得分:4)
这似乎可行:
@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中看到。
答案 1 :(得分:0)
完全是: 例如
PUT method #1
@RequestMapping(method=RequestMethod.PUT, value="/foo",
headers="returnType=Foo")
public @ResponseBody Foo updateFoo(@RequestBody Foo foo) {
fooService.update(foo);
}
//PUT method #2
@RequestMapping(method=RequestMethod.PUT, value="/foo",
headers="returnType=FooExtra")
public @ResponseBody FooExtra updateFoo(@RequestBody FooExtra fooExtra) {
fooService.update(fooExtra);
}
您在这里获得文档: adding custom header