3

org.springframework.hateoas.ResourceSupport already contains a getId method.

In case my resource (or better domain object) contains an integer id which needs to be presented to the client I cannot add a simple id and add getId/setId (as ResourceSupport already has it).

Should I return something <myResourceObjectName>_id or is it better to just let the client interpret the id link/URL provided by ResourceSupport?

Soumitri Pattnaik
  • 3,246
  • 4
  • 24
  • 42
Marcel Overdijk
  • 11,041
  • 17
  • 71
  • 110

2 Answers2

4

For the purposes of the client, the URL the resource was initially retrieved from (via a GET) is its id. No interpreting should occur and the client should treat the URL as an opaque string.

Jonathan W
  • 3,759
  • 19
  • 20
  • But what I need to get to show the id in the client? – Marcel Overdijk Oct 01 '14 at 12:58
  • You're missing the point. You don't need anything other than the URL to identify the resource, therefore use the URL as the id. – Jonathan W Oct 02 '14 at 04:35
  • 1
    I do get the point. But in some cases you just need the actual id from table to present to the customer as it has a meaning. E.g. a unique order id generated and part of e.g. an order resource. – Marcel Overdijk Oct 02 '14 at 12:24
  • The point is that it is *not* supposed to have meaning to the client. If you need to sort resources in a collection, you should pass sort parameters to the defined relation. – Jonathan W Oct 02 '14 at 15:10
  • 2
    EG: for an order id, it's better to have your API return a title field that has the order Id in it. As it's really the title of the order the client is trying to present...it just so happens that title is ALSO the order id your backend uses. Clients should not be built on top of keys defined in server systems...only with URL's. It's a bit tricky to get used to but allows for a really nice decoupling. – Chris DaMour Oct 07 '14 at 19:56
1

You migth want to check this article how-to-expose-the-resourceid-with-spring-data-rest

 @Configuration
public class MyCoolConfiguration extends RepositoryRestMvcConfiguration {

    @Override
    protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.exposeIdsFor(FooEntity.class);
    }
}