View video tutorial

SPRING BOOT RESTful API

SPRING BOOT

Restful API is an architectural style that represents standard HTTP methods for handling client endpoint requests.

Spring Boot RESTful API


➔ Spring Boot is the most popular and widely used Java framework that simplifies the process of building RESTful APIs by providing auto-configuration, dependency injection, embedded servers, and many more options.

Key Features of Spring Boot RESTful API:


➔ RESTful (Representational State Transfer) APIs are architectural representations or styles that follow standard HTTP methods for handling client operations on server resources using client endpoints.

➔ The standard HTTP methods followed in most cases in RESTful operations are GET, POST, PUT and DELETE.

➔ In Spring Boot, many aspects of application configuration are automatically configured based on dependencies added to the project classpath. For example, if the spring-web dependency is specified, an embedded server is configured.

➔ @RestController: An annotation is used on a class to define the class as a controller for handling HTTP client requests and returning responses in JSON or XML format.

➔ @SpringBootApplication: This annotation is used to scan and automatically configure any dependency components of any class in the application. The @SprigBootApplication annotation actually combines three annotations, namely @ComponentScan, @Configuration, @EnableAutoConfiguration. So there is no need to use any of them separately.

Steps to follow:

To create a RESTful API in Spring Boot, the following steps need to be performed, starting from project setup to testing the RESTful API.


➔ Project Setup: Create a project in STS (Spring Tool Suite), select spring-mvc as a key dependency.

➔ Define a model: A simple Java class is created. An @Entity annotation is used on the class, and @Identity is used on a key property as a primary key for the database table.

➔ Create a database layer: A Java interface is defined to create a repository that extends JpaRepository to get built-in CRUD support from JpaRepository.

➔ Create the service layer: Create a Java class with the @Service annotation so that Spring Boot can treat this class as a service and configure it accordingly. This service class interacts with the repository.

➔ Create Controller: Create a Java class with @RestController annotation and this class will define all API endpoints for client requests using @GetMapping, @PostMapping, @PutMapping, @DeleteMapping. These annotations are mapped to all standard HTTP methods such as GET, POST, PUT, DELETE.

➔ Run the application: Run the application class annotated with @SpringBootApplication. This will eventually run the project and eventually run the embedded server (such as Tomcat) to serve client requests.

➔ Test the RESTful API: Open a browser, type the endpoint in the address bar, and press Enter to see the response. Alternatively, API endpoints can be tested using API testing tools such as Postman.