What is the best practice for obtaining an entity from a link in spring-hateoas
?
I have two independent entities, author
and document
, where documents have can have multiple authors, but authors can exist without being linked to any documents.
To fetch the authors for a given document, there is an endpoint at /documents/{id}/authors
which returns a list of the authors and their links.
An author is added to a document by issuing a POST
of a link entity to /documents/{id}/authors
where the entity contents are the same as a org.springframework.hateoas.Link
, that is to say a rel
and an href
:
{
"rel": "author",
"href": "http://localhost:8081/authors/50"
}
I want to be able to do a service call like:
service.addAuthor(documentId, authorId);
documentId
is provided as a @PathVariable
, while authorId
is embedded in the href
.
In order to get authorId
, I currently use spring's RequestMappingHandlerMapping
and the controller class for the entity type (author in this case) to fetch the RequestMapping
for the entity's GET method and fetch the id
uri template variable for the href
provided in the link. Finally, I have to parse this as a number.
Is there some better/built-in way of doing this?
Using spring-hateoas
0.17.0, if that is relevant.