BigDong BigDong - 5 months ago 36
reST (reStructuredText) Question

is PersonResource the same thing as Person in spring-hateoas

I have a
class that contain id,firstName,lastName:

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

Is it the same thing as
How should I do for my id, has
already have a
method implemented.

Answer Source

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 {
    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" : ...
            "href":"http://localhost:8080/person/1" // the resource id

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

public class CustomRepositoryRestConfiguration extends RepositoryRestConfigurerAdapter {

    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration configuration) {
        configuration.exposeIdsFor(Parameter.class, Reference.class, Task.class);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download