Adding HTTP Method to Spring HATEOAS Links
I am wondering if it is possible to add an HTTP method to links created with Spring HATEOAS. I would like the link to look something like this:
{
"href":http://localhost:8080/admin/users",
"rel": "add",
"method": "POST"
}
{
"href":http://localhost:8080/admin/users/john",
"rel": "remove",
"method": "DELETE"
}
I couldn't find anything that would allow me to add a "method" to a link.
source to share
This makes no sense. href
indicates the address of some resource, and rel
indicates how it is associated with the current resource. The HTTP method indicates what the client wants to do with it, which is not part of the relationship between the two.
In your example, the relation "remove"
doesn't make sense: HTTP indicates the verb DELETE
, and the semantics
DELETE http://localhost:8080/admin/users/john
are already known. Likewise, it POST
creates a new resource, so specifying is /admin/users
enough for the client to be able to specify users (using GET
) or add a new user (s POST
).
source to share
You must use the "edit" relationship.
See section (11.1) from the Atom Pub RFC ( http://tools.ietf.org/html/rfc5023 ) that defines that you can send PUT / DELETE / GET requests to this edit relationship URI.
source to share
Consider using HTTP OPTIONS on the URIs you specify. This will respond with valid parameters for this resource. This isn't often done, but for me OPTIONS is a great way to help clients understand what's allowed.
Great blog post here: http://zacstewart.com/2012/04/14/http-options-method.html
source to share
The only link relationship should be able to tell clients how to use the link, so I think the parameter method
in the link would be redundant. The reference relationship definition must contain information about valid HTTP methods.
Also, I think the parameter approach method
makes your system more confusing and error prone.
Let's say you have a link relationship do-something
that was originally meant to be POST only. Then the creator do-something
wants to change this stat and only switch to PUT. Having a parameter method
in links makes you prone to inconsistency between the "source of truth" (defining the link relationship) and the servers that provide the links.
source to share
I have developed a REST API based on the Richardson model; http://martinfowler.com/articles/richardsonMaturityModel.html
All endpoints returned "allowed" links using the HTTP method. This design approach allowed the consumer to know which HTTP method to use, rather than calculating which method to use. This was useful because some operations used POST or PUT. POST is used to create a resource or change the state of a resource. PUT was used to update an existing resource and delete to delete a resource.
Including an HTTP method in a link is a powerful mechanism that lets the API consumer know how to call the API.
source to share