CRUD Rest Service With Spring Boot and JPA

CRUD Rest Service With Spring Boot and JPA:-

Spring framework has become a platform for almost every development task.You can set up a project and start building very fast.In this article, we are going to build a CRUD Rest Service With Spring Boot and Jpa to manage users.We are going to use the same code throughout our series on spring boot and test the application using a RestClient “Postman”. Please visit my previous post and do necessary steps here to create a project.I request you to keep patience as this article is gonna be quite long.

So, Let’s get Started !!

Project Structure:-

We are creating different packages for Beans, Controllers, Repository, and Services.Let’s go ahead and create following package structure as shown below.We are going to see each and every package and its functionalitites one by one with code.

Spring Boot Rest




SampleRestApplication.java:- 

It is the entry point of Spring Boot application.We are going to  annotate our class with @ComponentScan  and indicate the base package to scan for spring beans. It contains @SpringBootApplication annotation which is basically a combination of the following annotations.

  1. @Configuration
  2. @EnableAutoConfiguration
  3. @ComponentScan

We are using two more annotations:-

  1. @EntityScan:- This annotation Scans for all persistence based beans in your defined package.
  2. @EnableJpaRepositories:-  This annotation enables Spring Data JPA repositories support in your application.

User.java:-

We are going to create a simple bean to accept input and return as an output.




Configure Mysql Database:-

We are already adding the spring-data-jpadependency in our maven configuration. So spring-data-jpa is already in the classpath. It automatically tries to configure a DataSource by reading the database specific properties from the file.application.properties

Let’s add the database configuration in fileapplication.properties and spring automatically going to load at runtime. Create a database schema called springboot in MySQL .

Replace your database credentials in properties spring.datasource.username and spring.datasource.password . The property  spring.jpa.hibernate.ddl-auto can have two values create-drop and update  . create-drop will drop and recreate the existing database entity whereas update  will update the existing database entity if any changes are there.

Create Domain Classes or Entity:-

We now create our User entity which is going to persist in our database. Our entity is going to have following set of fields.

  1. id is  pk of the entity
  2. firstname
  3. lastname
  4. institute

User.java:-

As we can see above , we are creating two User classes. User class as a simple plain POJO object and use it for sending as a response Object. Another One is User entity class because, we don’t want to expose our entity object directly as a response, hence we are creating another POJO  User object.


Access Users Data From Database:-

We are creating UserRepository to access data from database persisted by hibernate.Our repository class is going to extend JpaRepository.

Spring Data JPA aims to significantly improve the implementation of data access layers by reducing the effort to the amount that’s actually needed. As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically.

UserRepository.java:-

You will now be able to use JpaRepository’s methods like save() , delete(),findAll(),delete() etc. The repository above extendsJpaRepository , passes the JPA entity and its primary key.

Basic methods for finding a single record, all records, paginated records, create/update, and delete are automatically provided. It’s also very easy to overload any custom query to add pagination and sorting.

The methodfindByfirstname lets Spring Data JPA automatically generate a like query for the first name column. We can also write query in JQL and execute like below in case we need it.

Writing Our Business Service:-

We are almost done setting up our JPA , now we are going to use the repository and write our business logic.So we are going to do crud operations and declare our service methods in an interface and write separate implementations.

UserService.java:-

The above interface gives you an idea how our business logic is going to be implemented , let’s take a pause and write business service implementations one by one while creating Rest Implementations.

Writing Rest Implementations:-

Now we are actually going to do the rest implementations of the API.Let’s Open our HelloController and start implementing one by one. We are first going to write code in our RestController and call business service class UserService from the controller for a particular endpoint.Create a class named UserServiceImpl implements UserService to write our business logic and implement all the methods.

Spring 4.0 introduced @RestController, a specialized version of the controller which is a convenience annotation that does nothing more than adding the @Controller and @ResponseBody annotations. By annotating the controller class with @RestController annotation, you no longer need to add @ResponseBody to all the request mapping methods. The @ResponseBody annotation is active by default.

Testing and Execution:-

Let’s see now how we are going to give final shape to our CRUD Rest Service With Spring Boot and JPA step by step.

1. Create a User:-  Method is Post and EndPoint is /users

Add following line of code in HelloController.java

This method tells us that the above code will be executed once a Post request with url is localhost:8080/users requested. We are using @RequestBody which says that it expects user object in the body as JSON format.

Now that we have added our request mapping and  write business logic in UserServiceImpl.java.

We are copying properties from User entity object to User Pojo using BeanUtils.copyProperties and returning a Pojo object from our service class.

Testing:-

 

2. Retrieve a User:-  Method is GET and EndPoint is /users/{id}

Add following line of code in HelloController.java

This method tells us that the above code will be executed once a GET request with url is localhost:8080/users/1 requested. We are using RestEntity which says that returns a User Object .

Now that we have added our request mapping and  write business logic in UserServiceImpl.java.

We are copying properties from dbUser entity object to User Pojo using BeanUtils.copyProperties and returning a Pojo object from our service class.Lets run our Project using mvn Spring-boot:run

Testing:-

GET Request Spring Boot

 

3. Update a User:-  Method is PUT and EndPoint is /users/{id}

Add following line of code in HelloController.java

This method tells us that the above code will be executed once a PUT request with url is localhost:8080/users/1 requested. We are using ResponseEntity which says that returns any Object .

Now that we have added our request mapping and  write business logic in UserServiceImpl.java.

Testing:-

Pur

3. Delete a User:-  Method is DELETE and EndPoint is /users/{id}

Add following line of code in HelloController.java

This method tells us that the above code will be executed once a DELETE request with url is localhost:8080/users/1 requested. We are using ResponseEntity which says that returns any Object.

Now that we have added our request mapping and  write business logic in UserServiceImpl.java.

Testing:-

Download Code

,

4 thoughts on “CRUD Rest Service With Spring Boot and JPA

Leave a Reply

Your email address will not be published. Required fields are marked *