Creating Rest API using Spring Boot

Posted By : Parvinder Singh | 03-May-2022

API - An (API) stands for Application Programming Interface. It establishes a connection between computers by providing them with readily available codes and information to them. It is a type of software interface that acts as a mediator among other pieces of software to streamline the interaction with one another.

REST API - A REST API (known as RESTful API) is an application programming interface that conforms to the constraints of REST architectural style and allows them to interact with RESTful web services.

Spring Boot - Spring Boot is a Java-based framework that is used to create stand-alone, production-grade Spring-based Applications. Spring is a huge framework that lets us write enterprise in JAVA applications, and Boot is a bootstrap that lets us bootstrap a Spring application from scratch. This is how it is named Spring Boot.

Building Spring Boot REST API in Java -

Step 1: Initializing a Spring Boot Project - To start with Spring Boot REST API, you first need to initialize the Spring Boot Project. You can easily initialize a new Spring Boot Project with Spring Initializr.

Step 2: Connecting Spring Boot to the Database - Next, we need to set up the Database, and we can do it easily with the help of Spring Data JPA.
We can add some elementary information in your application.properties file to set up the connection to our preferred Database after that we can add our JDBC connection URL and provide a username and password for authentication, and then we can set the ddl-auto property to update.

Step 3: Creating a User Model - The next step is to create a Domain Model that is also called Entities and is annotated by @Entity.
We can create a simple User entity by annotating the class with @Entity. We can use @Table annotation to specify the name for our Table. @Id annotation is used to annotate a field with Id and set it to @GeneratedValue for id to AUTO GenerationType.

Step 4: Creating Repository Classes - To perform CRUD operations (Create, Read, Update, and Delete)  on the User entities, we need to have a UserRepository. For this we have to use the CrudRepository extension and annotate the interface with @Repository.

Step 5: Creating a Controller - Spring @Controller annotation is a specialization of @Component annotation. The @Controller annotation indicates that a particular class serves the role of a controller. Spring Controller annotation is typically used in the combination with annotated handler method that is based on the @RequestMapping annotation. It is applied to classes only. It’s used to mark a class with a web request handler.

Step 6: Compile, Build and Run -  We run the Spring Boot REST API we have created. To run the application directly we execute ./mvnw spring-boot:run on the command line from our project folder where the pom.xml is located. If our application has successfully run, we will be able to see the audit logs at the end of the command line.

Step 7: Testing the Spring Boot REST APIs - Spring Boot REST API is now running on http://localhost:8080/.
we can change the port of Spring Boot from your application.properties file 8080 is the default port that Spring Boot runs in.

Conclusion -REST API is a unique type of API  among cloud applications thus REST API's are all about communication. 

About Author

Author Image
Parvinder Singh

Parvinder Singh is a Developer , He is sincere and hardworking He loves travelling and exploring new places.

Request for Proposal

Name is required

Comment is required

Sending message..