服务版本控制: 借助header:Accept 和 header: ContentType 实现
For example, to retrieve a user in JSON format:
version:1
- Request
GET http://api.example.com/users/12345 Accept: application/json; version=1 - Response
HTTP/1.1 200 OK
Content-Type: application/json; version=1
{“id”:”12345”, “name”:”Joe DiMaggio”}
version:2
Now, to retrieve version 2 of that same resource in JSON format:
- Request
GET http://api.example.com/users/12345 Accept: application/json; version=2 - Response
HTTP/1.1 200 OK
Content-Type: application/json; version=2
{“id”:”12345”, “firstName”:”Joe”, “lastName”:”DiMaggio”}
服务版本控制方法2:直接在uri中添加版本
通过给uri添加版本号的方式,违反了 REST 设计约束, 这种方法不建议,
反对意见认为: uri统一资源标识符, 既然资源没有变, uri也不该变
本文探讨了两种服务版本控制的方法:一种是通过请求头中的Accept和ContentType字段来指定版本,另一种是在URI中直接添加版本号。前者更符合REST设计原则,后者虽简单但不推荐。
576

被折叠的 条评论
为什么被折叠?



