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:
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.
Deepak is a Full-stack developer with around 3 years of experience. He is an expert in building Java integrated web applications, Creating REST APIs with well designed, testable and efficient and optimized code. He is having experience in web technologies like AngularJS, Angular6, JavaScript, etc. He is a Lead developer at Codegrip. He loves to solve technical problems and helping others.
Get the latest updates by sharing your email.
Flexible Engagement Model | Secure & Scalable Apps | First Time Right Process
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
We had very close go live timeline and MindBowser team got us live a month before.
They were a very responsive team! Extremely easy to communicate and work with!
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Mindbowser was very helpful with explaining the development process and started quickly on the project.
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Mindbowser is professional, efficient and thorough.
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
MindBowser was great; they listened to us a lot and helped us hone in on the actual idea of the app.” “They had put together fantastic wireframes for us.
They're very tech-savvy, yet humble.
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
As a founder of a budding start-up, it has been a great experience working with Mindbower Inc under Ayush's leadership for our online digital platform design and development activity.
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
They are focused, patient and; they are innovative. Please give them a shot if you are looking for someone to partner with, you can go along with Mindbowser.
We are a small non-profit on a budget and they were able to deliver their work at our prescribed budgets. Their team always met their objectives and I'm very happy with the end result. Thank you, Mindbowser team!!