REST - What exactly is meant by Uniform Interface?

前端 未结 4 1534
无人共我
无人共我 2021-01-30 02:34

Wikipedia has:

Uniform interface

The uniform interface constraint is fundamental to the design of any REST service.[14] The uniform i

4条回答
  •  自闭症患者
    2021-01-30 03:20

    The Uniform Interface constraint, that any ReSTful architecture should comply with, actually means that, along with the data, server responses should also announce available actions and resources.

    In chapter 5 of his dissertation, Roy Fielding (originator of the ReST architectural style) states that the aim of using uniform interfaces is to "ease and improve global architecture and the visibility of interactions".

    In other words, querying resources should allow the client to request other actions and resources without knowing them in advance.

    JSON-API spec offers a good example:

    {
      "links": {
        "self": "http://example.com/articles",
        "next": "http://example.com/articles?page[offset]=2",
        "last": "http://example.com/articles?page[offset]=10"
      },
      "data": [{
        "type": "articles",
        "id": "1",
        "attributes": {
          "title": "JSON API paints my bikeshed!"
        },
        "relationships": {
          "author": {
            "links": {
              "self": "http://example.com/articles/1/relationships/author",
              "related": "http://example.com/articles/1/author"
            },
          },
          "comments": {
            "links": {
              "self": "http://example.com/articles/1/relationships/comments",
              "related": "http://example.com/articles/1/comments"
            }
          }
        },
        "links": {
          "self": "http://example.com/articles/1"
        }
      }]
    }
    

    Just by analysing this single response, a client knows:

    1. What was queried (articles in this example)
    2. How are structured articles objects (id, title, author, comments)
    3. How to retrieve related objects (i.e. the author, list of comments)
    4. That there are more articles (10, based on current response length and pagination links)

    I Hope this helps.
    For those passionate about the topic, I strongly recommend reading Roy Thomas Fielding's dissertation!

提交回复
热议问题