0

I have a Person.java class that contain id,firstName,lastName:

According to the documentation, if I wish to have hateoas link, pagination and count, I should use a PersonResource:

https://docs.spring.io/spring-hateoas/docs/current/reference/html/#fundamentals.resources

Is it the same thing as Person ? How should I do for my id, has ResourceSupport already have a getId() method implemented.

Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204

1 Answers1

1

The id of your domain object and the id of the REST resource are two completely different things.

As mentioned in the Spring HATEOAS API documentation, a Resource is a wrapper around a domain object that adds link to it. A Resource is a fundamental concept of REST. It's an object with a type, associated data, relationships to other resources, and a set of methods that operate on it.

Basically, its id is the URL you use to interact with the GET/PUT/POST/DELETE methods.

Wrapped into the resource (PersonResource), is your domain object (Person), a POJO with properties and getters/setters :

// simple definition of a Person Resource in Spring
public class PersonResource extends Resource<Person> {

    public PersonResource(Person content, Link... links) {
        super(content, links);
    }

}

public class Person {
    ...
    String name;
    Integer age;
    // getters/setters omitted for clarity
}

A REST API is generally used to access and update data stored in a database table (SQL) or collection (NoSQL). Such an entity has a unique id, that you map against the id property of your POJO:

public class Person {
    @Id
    String id;
    String name;
    Integer age;
    // getters/setters omitted for clarity
}

By default, when you interrogate your REST API, Spring Data Rest won't even expose your entity id (it is meaningless in a REST context, what's important is how you identify the resource):

GET http://localhost:8080/person/1

{
    "name":"Ron Swanson",
    "age" : ...
    "_links":{
        "self":{
            "href":"http://localhost:8080/person/1" // the resource id
         }
    }
}

FYI, the entity id can be provided if you tweak the configuration :

@Configuration
public class CustomRepositoryRestConfiguration extends RepositoryRestConfigurerAdapter {

    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration configuration) {
        configuration.exposeIdsFor(Person.class);
    }

}
Marc Tarin
  • 3,109
  • 17
  • 49
  • Thanks @MarcTarin a lot for all these explanation. Do you know if this work with `@Controller` or only `@RestRepository` ? Is it on purpose that you use `Resource` and not `ResourceSupport` ? Could you also explain the difference of the two ? – Dimitri Kopriwa Sep 21 '17 at 09:13
  • 1
    Check the API doc : [ResourceSupport](https://docs.spring.io/autorepo/docs/spring-hateoas/0.19.0.RELEASE/api/org/springframework/hateoas/ResourceSupport.html) is the base class of [Resource](https://docs.spring.io/autorepo/docs/spring-hateoas/0.19.0.RELEASE/api/org/springframework/hateoas/Resource.html). I don't know what '@RestRepository' is. you should favor [@RestController and @RepositoryRestController](https://stackoverflow.com/questions/34512878/difference-between-restcontroller-and-repositoryrestcontroller) over `@Controller` in a REST context. – Marc Tarin Sep 21 '17 at 09:25
  • Ok I am playing with your information, I am trying to do a getList method in my `@RestController`, but I can't find the proper synthax to convert my List into a `Resource>`, would you mind also adding an example for that. Thanks a lot – Dimitri Kopriwa Sep 21 '17 at 09:40
  • A very special thanks for showing how to expose the id. – Dimitri Kopriwa Sep 21 '17 at 09:46
  • What you're looking for is [Resources](https://docs.spring.io/spring-hateoas/docs/current/api/org/springframework/hateoas/Resources.html), or even "better", [PagedResources](https://docs.spring.io/spring-data/commons/docs/current/reference/html/#core.web.pageables). [This](https://stackoverflow.com/a/29924387/5873923) may help to. If you encounter difficulties, open another thread with an example of what you tried, that helps keeping each thread specific enough. – Marc Tarin Sep 21 '17 at 10:04
  • I have created a new question for that: https://stackoverflow.com/questions/46342114/how-to-write-paginated-controller-that-expose-resource-or-list-of-resource-in-sp – Dimitri Kopriwa Sep 21 '17 at 10:55