Finding Residual Resource When Using HATEOAS?

When you read about the HATEOAS / Hypermedia limitation, I often see that a resource must have its own / href. The argument for this is that the client does not need to know how to create a URL for this particular resource, for example. to update the resource.

eg.

{
     //instead of "id":"123"
     "href":"/api/v1/orders/123",

     order state...
}

I like this idea.

But how is this concept suitable for data collection? Let's say I need to receive an order with a specific order ID, how will the customer handle this? I still need to know how to create a resource url in this case, right?

How should a client know where and how to look for a resource? Should he know about the API url anyway?

+4
source share
4

API HATEOAS , .

HATEOAS, , URI . URL , .

. , , . URL- , . HATEOAS URL- , , . URL- : " ", .

HATEOAS - , , - , . , URL-, .

, --get-over-the-wire - (REST REpresentational State Transfer). , , , . .

, :

{
  "id": "/api/v1/orders/123", // reference to the current resource
  "rel": {
    "cancel": {
      "url": "/api/v1/orders/cancel?order_id=123",
      "method": "POST",
      // Metadata about what the cancel operation returns...
    },
    "list_orders": {
      "url": "/api/v1/orders",
      "method": "GET",
      // Metadata about what the list_orders operation returns...
    },
    // ...
    // Other operations available to the owner
  },
  // ...
  // Order state
}

, HATEOAS, "rel" "cancel" "list_orders" .

, , , cancel, .

+4

, , -: . HTML HTML- . JSON, ​​ , . URI , -, , .

- :

{ "order": {
    "link": {
        "template": "https://your-api.com/orders/{id}",
        "method": "GET",
        "type": "application/json"
    }
  }
}

, - , "id" . , , URI. , Ive ; , , , , . . ( ) + json-.

+5

URI, , , . , API , HATEOAS 3 API RESTful.

, , , , .

RESTful API , -, . , , , , . :

{
    "orders": [
        {
            "date": "2015-01-25",
            "total": 1234,
            "links": [
                {
                    "rel": "order",
                    "href": "https://follow.the.link"
                }
            ]
        },
        {
            "date": "2015-01-22",
            "total": 1337,
            "links": [
                {
                    "rel": "order",
                    "href": "https://follow.this.other.link"
                }
            ]
        }
    ]
}

, , , "".

RESTful Web Services Cookbook .

+5

. . (, URL). , .

, .

. , . , HATEOAS, URL-, .

, . , . URL-.

, .

+1

All Articles