Developing a Rest API with Spring boot

·

4 min read

Developing a Rest API with Spring boot

Recently, I built a Rest API with spring boot. It was my second application, and I am proud of myself for this one. Only because, I have learned a lot while building this. I will be sharing the details about the application, my learnings from it, and what I am planning to learn in my next project with you in this blog. I am also planning to write more in-depth blogs explaining these concepts in future.

The Application

It was a Rest API of local businesses where you can search businesses by the category field of the businesses and also with any keyword.

Here is the link to my application's GitHub repository.

github.com/vishy312/springboot-apis/tree/ma..

The data was to be persisted on an external PostgreSQL database. By external database here, I mean that the database that is used to store all the data of local businesses should not be an embedded one, because spring boot also gives you the luxury of the database, which is okay for development purposes, but not so much for production. Following is the basic configuration to set up PostgreSQL in spring boot.

I used the Spring Data JPA library to deal with all the persistence operations. Spring Data JPA is a library that gives you various methods to do all the database-related operations like calling a list of local businesses from the database, adding more local businesses, or editing an existing one. You don't have to worry about SQL queries most of the time. Mind you, the Spring Data JPA is not the only option for this purpose, one of those options is Spring JDBC.

The Rest API in spring boot is based on the MVC structure. where The Entity class, which is like a POJO class of the local business, is the M(model) in MVC. The service class, which holds all the logic, is the C(controller) in the MVC.

V(view) is not present in the Rest API, rightly so, because a Rest API is for the backend purpose, and there is no need for a View component.

There were three GET methods in the application, one for getting all businesses, one for getting businesses by category, and one for getting by a keyword. There were two POST methods for the flexibility of adding new local businesses either one by one or all at once. Besides, there was one each of a PUT method, a PATCH method, and a DELETE method.

In the Repository interface, two additional custom methods were created, one for getting businesses by category, and the other for getting by a keyword. Yes, you can create custom methods in the repository, without giving any implementation of yours. This is a feature that Spring Data JPA provides you, but you should follow a protocol to do that correctly.

I used Pagination on the GET methods to improve performance. Pagination is when you break the returned set of data into small chunks called Pages. getting data in small chunks to improve the performance of the application by a great margin.

My Learnings From It

I have learned a lot while building this application and truly understood the power of learning by doing. Following are some of my top learnings from building this application.

  • I have learned how to do Pagination and Sorting in spring boot.

  • I have learned how to do Error Handling in spring boot and how important it is to implement in your rest API. Spoiler alert, it is very important.

  • What is a PATCH method and how to implement it.

  • It is so much better to return Response Object instead of sending only the entity to the client. Because, if anything goes wrong, the Response Object will tell the client what went wrong actually.

Concepts I Will Focus On In My Next Project

With my next application in spring boot, I hope to learn the following concepts

  • ThymeLeaf Integration

  • @Query annotation

  • Query By Example

Okay, that is it for this blog, untill next time