Creating a Rest API in Spring Boot And Hibernate

Spring Boot is a powerful framework that makes it easy to create RESTful APIs. In this article, we will go through a step-by-step guide on how to create a RESTful API in Spring Boot with MySQL and JPA. We will start by creating a new Spring Boot project and configuring it for our needs.

Read more

Step 1: Setup Development Environment

Read more

Before we begin, we need to set up our development environment. We will need to install the following software:

Read more
  • Java Development Kit (JDK)
  • Intellij (or any other preferred IDE like Eclipse)
  • MySQL Server
Read more

Step 2: Create a Spring Boot Project

Read more

The first step is to create a new Spring Boot project using the Spring Boot Initializer.

Read more
  1. Open your web browser and go to Spring Initializer.
Read more
Read more

Spring Initializer — https://start.spring.io/

Read more
  1. Set the following options
  2. Project: Maven Project/Gradle according to your needs
  3. Language: Java
  4. Spring Boot: 2.6.2 (or the latest version)
  5. Group: com.boot
  6. Artifact: spring boot API/any desired info of your own choice
  7. Description: Demo Project for Spring Boot API/ any information you like about the project
  8. Packaging: Jar
  9. Java: 11
Read more

2. Click on the “Add Dependencies” button and add the following dependencies

Read more

i) Spring Web

Read more

ii) Spring Data JPA

Read more

iii) MySQL Driver

Read more

Select these dependencies and click on the Generate button.

Read more

Extract the downloaded project ZIP file to your preferred location. And import it in your IDE.

Read more

Step 3: Create Entity Class

Read more

Next, we need to create an entity class to represent our data model. In this example, we will create a “Product” entity class.

Read more
  1. Right-click on the “com.boot” package and create a package called entity inside it.
  2. Enter “Product” as the class name in the “entity” package and click on the “Finish” button.
  3. In the “Product” class, add the following code:
Read more

The @Entity annotation specifies that this class is an entity and should be mapped to a database table. The @Table annotation specifies the name of the database table that will store instances of this entity. The @Id annotation specifies the primary key of the entity, while the @GeneratedValue annotation specifies that the primary key should be generated automatically.

Read more

Step 4. Create Repository Interface

Read more

Now, we need to create a repository interface to handle database operations for the “Product” entity.

Read more
  1. Right-click on the “com.boot” package and create a package called repository or repo.
  2. Enter “ProductRepository” as the interface name in the package repository or repo and click on the “Finish” button.
  3. In the “ProductRepository” interface, add the following code:
Read more

The @Repository annotation specifies that this interface is a repository, and Spring will create an instance of it automatically. The JpaRepository interface provides a set of methods for performing CRUD(Create, Read, Update, Delete) operations on the “Product” entity.

Read more

Step 5: Create A Service Class

Read more

Next, we need to create a service class to handle the business logic for our REST API.

Read more
  1. Right-click on the “com. boot” package and create another package called service.
  2. Enter “ProductService” as the class name in the service package and click on the “Finish” button.
  3. In the “ProductService” class, add the following code:
Read more

For saving a product in the database we will use the following code:

Read more

To get all products from the database we will use the following code:

Read more

For getting a single product from the database we will use the following code:

Read more

For updating a single product from the database we will use the following code:

Read more

For deleting a single product from the database we will use the following code:

Read more

Step 5: Create Controller Class

Read more

Next, we need to create a controller class to handle HTTP requests for our REST API.

Read more
  1. Right-click on the “com. boot” package and create another package called controller.
  2. Enter “ProductController” as the class name in the controller package and click on the “Finish” button.
  3. In the “ProductController” class, add the following code:
Read more

The @RestController annotation specifies that this class is a controller for RESTful API requests. The @RequestMapping annotation specifies the base URL for all requests handled by this controller.

Read more

Next, we need to add methods to handle HTTP requests. In this example, we will add methods to handle GET, POST, PUT, and DELETE requests.

Read more

For Post Request, we will be using the following code:

Read more

The @PostMapping annotation is used to indicate that this class will handle HTTP Post requests and return the response as JSON. It is used to map the /api/v1/products path to this class.@RequestBody is an annotation in Spring Framework used to bind the HTTP request body to a parameter in a controller method. When a client sends an HTTP POST or PUT request, it may include data in the request body. This data is typically in JSON or XML format and contains information about the resource being created or updated.

Read more

for Get Request all the products, we will be using the following code:

Read more

The @GetMapping annotation is used to indicate that this class will handle HTTP Get requests and return the response as JSON. It is used to map the /api/v1/products path to this class. Here the getAllProducts() method fetches all the products and has a path /products.

Read more

for Get Request of a single product, we will be using the following code:

Read more

The@PathVariable annotation is used to extract data from the URL path of an HTTP request. It is used to capture dynamic segments of a URL and map them to a method parameter in a Spring Boot controller. getProductById() method is used to get a product by id and has a path /products/{id}.

Read more

For Update Requests, we will be using the following code:

Read more

In this example, we’ve added a @PutMapping annotation for the updateProduct() method. The @PutMapping annotation is used to map HTTP PUT requests to the /product/{id} endpoint, where {id} is a path variable for the product ID. The @RequestBody annotation is used to bind the request body to the product parameter in the method. When a PUT request is made to /api/v1/product/{id}, the updateProduct() method will be executed with the id parameter set to the product ID from the URL path and the product.

Read more

For Delete Requests, we will be using the following code:

Read more

Now, we are completed with the programming side and just remain with the database and then test the endpoints and then we are done.

Read more

First of all, we will have to configure MySql in our application.properties files.

Read more

we will add the following code in the application.properties file

Read more
Read more

Now, after this,

Read more

we will test our API endpoints in Postman. The default port for Spring Boot is 8080. You can change the port by using the command

Read more

inside the application.properties file

Read more

In my case, the port is 8080.

Read more

In my case, For our Post Request, the endpoint will be like “http://localhost:8080/api/v1/product” and the output is:

Read more
Read more

Post request

Read more

For our Get Request, the endpoint will be like “http://localhost:8080/api/v1/product” and the output is:

Read more
Read more

Get All Products

Read more

For our Get By Id Request, the endpoint will be like “http://localhost:8080/api/v1/product/id” and the output is:

Read more
Read more

Get By Id

Read more

For our Update Request, the endpoint will be like “http://localhost:8080/api/v1/product/id” and the output is:

Read more
Read more

Update Request

Read more

And Finally,

Read more

For our Delete Request, the endpoint will be like “http://localhost:8080/api/v1/product/id” and the output is:

Read more
Read more

Delete Request

Read more

Did you like this story?

Please share by clicking this button!

Visit our site and see all other available articles!

Influencer Magazine UK