spring-projects / spring-data-rest

Simplifies building hypermedia-driven REST web services on top of Spring Data repositories
https://spring.io/projects/spring-data-rest
Apache License 2.0
905 stars 558 forks source link

[Question] Understanding links generated by Spring Data Rest #2388

Closed micama closed 1 week ago

micama commented 1 month ago

Hi

This is no issue but rather a question to understand the behaviour of Spring Data Rest.

Let's say I have an entity Person with the respective Repository extending JpaRepository. When acccessing the API's root URI, e.g.

http://localhost:8080/api

There is a link of the form for the person's resource

{
    "_links": {
        "persons": {
            "href": "http://localhost:8080/api/persons{?page,size,sort}",
            "templated": true
        }
    }
}

How is a client (web frontend or another backend service) supposed to handle the person's href? Should it be parsed, interpreted, cleaned up?

RESTful APIs are supposed to be self-describing and discoverable. So why is the curly braces part of the link's href? Should this not just be a valid, addressable URI, without having to parse or extract the actual usable part? Am I missing something here? How is the discoverability supposed to work here?

Furthermore the information on what is available is already present in the alps description. There, an interested client can read up on what's possible, etc.

Thanks

odrotbohm commented 1 week ago

The format SD REST returns is HAL. Clients would usually use a library that understands that format. The templated field indicates that the href value is a URI template. The variant you show contains optional request parameters, so – again – a library that understands URI templates would know how to expand that template into a traversable URI with parameter values given or not.

Going forward, please use StackOverflow for such general questions. Chances are much higher you find someone answering as it's drawing a much bigger audience than the tracker here.