美文网首页
RESTful Service Best Practices:

RESTful Service Best Practices:

作者: BenjaminCool | 来源:发表于2018-12-11 22:00 被阅读9次

服务版本控制: 借助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也不该变

相关文章

网友评论

      本文标题:RESTful Service Best Practices:

      本文链接:https://www.haomeiwen.com/subject/mflyhqtx.html