In previous article on REST API Development, we have already seen that how we can develop a REST API with minimum lines of code. Still, we have more scope to reduce lines of code using a concept called โSpring Data RESTโ in Spring Boot. So, our title of the article is โHow to reduce boilerplate code in REST API Spring Data RESTโ. In Spring Data REST concept we donโt have to write even RestController and its methods. The Spring Data REST dependency will take care of it using HATEOAS (Hypertext as the Engine of Application State).
HATEOAS project is a library of APIs that we can use to easily create REST representations. Spring Data REST provides hyperlinks to access REST operations with the help of HATEOAS only. When some details of a resource are requested, you will provide the resource details as well as details of related resources and the possible actions you can perform on the resource. HATEOAS provides us this type of details. You might have guessed that we will learn Spring Data REST concepts using Spring Boot only. Letโs start learning โHow to reduce boilerplate code in REST API Spring Data RESTโ.
What is Spring Data REST ?
Spring Data REST is a concept which minimizes a lot of manual work and provides basic implementation of CRUD functionality quite easy. It is built on top of Spring Data Project and also comes under the umbrella Spring Data Project.
How to configure Spring Data REST in Spring Boot Project ?
If you are using Maven tool, you need to add below dependency in your pom.xml which is the main dependency of our project. You will get feature of Spring Data REST from this dependency only. Itโs very easy to add if you are using Spring Boot Starter Project. You will find the same in steps given in below sections.
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
...
</dependencies>
Where can we use Spring Data REST Concept ?
The applications which require only CRUD operations, we can use this concept to generate REST API and get rid of boilerplate codes. As mentioned earlier using this concept we can reduce not only the boilerplate code, but also get rid of memorizing the annotations at Controller and method levels.
What are the steps to generate REST API using โSpring Data RESTโ in Spring boot ?
Step #1 : Create Spring Boot Starter Project
If you are new in creating Spring Boot Starter Project, follow a separate tutorial on โHow to create a starter project using STS?โ.
Step #2 : Add dependencies in Spring Boot Starter Project
Spring Data JPA -> to use JPA repository interfaces
Rest Repositories -> to use Spring Data REST features (Main part of our project)
MySQL Driver -> to get use of MySQL Database
Lombok -> to avoid lots of boilerplate code, for more info you may follow internal link on Lombok.
Spring Boot DevTools -> to avoid restarting tomcat every time we change in the code
If you are using Maven tool, you can check the below dependency in your pom.xml which is the main dependency of our project. As mentioned earlier, you will get features of Spring Data REST from this dependency only.
<dependencies> ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> ... </dependencies>
Step #3 : Write Model/Entity class
In order to generate the API, we are considering our previous entity โInvoiceโ.
Step #4 : Write Repository Interface
Interface InvoiceRepository extends from CrudRepository.
Step #5 : Update application.properties file
Thatโs all for the coding part we need to write. Now observe how simple it is !!!
How to test the created REST API using Spring Data REST ?
Step #1 : Run the created Spring Boot Starter Project
To run the application right click on Project and select Run As >> Spring Boot App.
Step #2 : Enter the URL as http://localhost:8080/ into Browser.
Once you enter the above URL and click enter, you will see the below output.
The above output indicates that your URL for REST API will be โhttp://localhost:8080/invoicesโ. The point to observe here is that it has taken โinvoicesโ in the path from the entity name (Invoice), converts the first letter to lowercase & adds suffix โsโ.
Step #3 : Create URL for CRUD operations
Below URLs will work perfectly for the created REST API.
Base URL = http://localhost:8080/invoices
GET URL : http://localhost:8080/invoices [to get all Invoices]
GET URL : http://localhost:8080/invoices/{id} [to get invoice with a particular id]
POST URL: http://localhost:8080/invoices [for saving invoices]
PUT URL : http://localhost:8080/invoices/{id} [to modify an Invoice with a particular id]
DELETE URL : http://localhost:8080/invoices/{id} [to delete an Invoice with a particular id]
Step #4 : Test the created API
One of the best and easiest way to test the REST API is to use โPOSTMANโ software. If you need more details on testing, go through a separate article on โHow to test REST API using POSTMAN?โ
For more details on Spring Data & Itโs sub-modules, kindly visit spring.io.
If you are in search of a good book, to get a best deal kindly visit books for Programming Languages.
Hmm is anyone else having problems with the images on this blog loading? Iโm trying to figure out if its a problem on my end or if itโs the blog. Any suggestions would be greatly appreciated.
Please let us know if others are also facing the same problem? We will fix it asap.
Hey there are using WordPress for your blog platform? Iโm new to the blog world but Iโm trying to get started and create my own. Do you need any html coding expertise to make your own blog? Any help would be really appreciated!
No. not needed.
Great ! ceshi