Learn About Spring Data Rest

Over the last few years, Spring has been updating its features. Among all of them, there is one Spring Data Rest. 

Spring Data REST is made on top of the Spring Data project. Because of that, it is easy to create HAL (Hypertext Application Language) applications.

Let’s see some features of Spring Data Rest:

  • Exposes a discoverable REST API for your domain model using HAL as a media type.
  • Supports Pagination
  • Supports Sorting or filtering
  • Can work with search resources etc

Here, we will see the basic example of Spring Data Rest and it’s working. I am assuming that you know the basics of Spring Boot. So let’s get started!!

We will be implementing one example using Spring Boot and MySQL, where we will create one User entity and see how the Spring Data Rest is used.

Now before starting we need to add one maven dependency,

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>

Now create one entity User.java

@Data
@AllArgsConstructor
@NoArgsConstructor


@Entity
public class User {

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Long id;

 private String name;
 private String email;

}

Now create a repository interface for the User entity.

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}

Once created the repository, we are done!!!! Yes, you read correctly. We are done. These are the single things we require to do to work with Spring Data Rest basic CRUD application.

Now let’s see what endpoints have been exposed by Spring Data Rest. To check this, you just need to hit get API in either browser or postman. In my case, the port is 8080 so the URL would be h

http://localhost:8080

Once you hit the localhost URL, you will get the following result, which will look like this.

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


   }
  }
}

The most beneficial part is that it runs the same as the Spring HATEOAS. So you will get the links and recommended links by default. 

Now let’s see some endpoints exposed by Spring Data Rest.

Get all users

URL:  http://localhost:8080/users

Response:

{
  "_embedded" : {
    "users" : [ {
      "name" : "Deepak",
      "email" : "deep@gmail.com",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/users/1"
        },
        "user" : {
          "href" : "http://localhost:8080/users/1"
        }
      }
    }, {
      "name" : "Prajapati",
      "email" : "praj@gmail.com",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/users/2"
        },
        "user" : {
          "href" : "http://localhost:8080/users/2"
        }
      }
    } ]
  },


 "_links" : {
    "self" : {
      "href" : "http://localhost:8080/users"
    },
    "profile" : {
      "href" : "http://localhost:8080/profile/users"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 2,
    "totalPages" : 1,
    "number" : 0
  }
}

Get User using pagination and sorting

URL: http://localhost:8080/users?page=0&size=2&sort=name,desc

Response

{
  "_embedded" : {
    "users" : [ {
      "name" : "Prajapati",
      "email" : "praj@gmail.com",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/users/2"
        },
        "user" : {
          "href" : "http://localhost:8080/users/2"
        }
      }
    }, {
      "name" : "Deepak",
      "email" : "deep@gmail.com",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/users/1"


 },
        "user" : {
          "href" : "http://localhost:8080/users/1"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/users?page=0&size=2&sort=name,desc"
    },
    "profile" : {
      "href" : "http://localhost:8080/profile/users"
    }
  },
  "page" : {
    "size" : 2,
    "totalElements" : 2,
    "totalPages" : 1,
    "number" : 0
  }
}

Save User

URL: http://localhost:8080/users/

Request Body:

{
    "name":"Ramesh",
    "email":"ramesh@gmail.com"

}

Response:

{
    "name": "Ramesh",
    "email": "ramesh@gmail.com",
    "_links": {
        "self": {
            "href": "http://localhost:8080/users/3"


  },
        "user": {
            "href": "http://localhost:8080/users/3"
        }
    }
}

Bonus

What if you want to add a search feature? For example, let’s consider what you want to search by name. For this simply add the JPA method to the repository.

public User findByEmail(@Param(“email”) String email);

That’s it!!! We have just created search by email functionality.

Now to use this, we just need to hit the URL like below.

URL: http://localhost:8080/users/search/findByEmail?email=deep@gmail.com

Response:

{
  "name" : "Deepak",
  "email" : "deep@gmail.com",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/users/1"
    },
    "user" : {
      "href" : "http://localhost:8080/users/1"
    }
  }

}

If you notice, we haven’t written any controller for any endpoint. The Spring Data Rest did everything. Of course, if you want to create a complex rest endpoint, you can not go with Spring

Data Rest, but this will create a basic CRUD application and save a lot of time. You can explore more about Spring Data Rest here.

Also, you can find the source code on my GitHub profile.

Keep Reading

Keep Reading

  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?