3

我们正在考虑在 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
4

2 回答 2

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:尚未测试,但在Spring boot 教程中看到。

于 2018-06-26T11:04:52.443 回答
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);
}

在这里您可以获得文档: 添加自定义标题

于 2018-06-26T11:09:24.847 回答