Powered by Blogger.

Wednesday 30 June 2021

A Rest API with Spring Boot using JSON, MySQL and JPA

2 comments :
Introduction:
In this article will look into how to create a REST API using HTTP methods for Create and Retrieve operations in Spring Boot along with MYSQL database. Spring Boot is an open source Java-based framework to build enterprise spring applications. . 
Overview: 
  • Create MYSQL database From WampServer.
  • Create the Spring Boot Project From STS-4.
  • Create Entity model class.
  • Create JPA Data Repository.
  • Create Service class.
  • Create Rest Controllers class.
  • Run the Project.
  • Testing using Postman.
  • Video Link for watching How to build API.

Create MYSQL database using WampServer.
Download WampServer install and Run-> go to phpMyAdmin and click.  
Open localhost/phpmyadmin/ 
Create Database Name is “hostel”.

Crate Table in hostel Database. Table Name is states with number of fields/Column.

Create Table column name, which is show in image.



Create the Spring Boot Project from STS-4:
It will write project name, group, artifact, version, description, package etc. 

 
Create Entity model class. 
  • Create the States model class to map with the States table.
  • @Entity is used to annotate that the class is an entity in the database.
  • @Table is used to annotate name of table in the database.
  • @id is used to reference that widget to your java file and access it from java file.
  • @GeneratedValue is used to generation strategies for the values of primary keys.
States.java

Create JPA Data Repository. 
  • Create StatsRepository Repository interface extending JPA Repository.
  • There are not need writing any SQL/MySql etc query.
  • @Repository annotation is used to indicate that the class provides the mechanism for storage, retrieval, search, and update and delete operation on objects.
StatsRepository.java

Create Service class. 
  • Create StateService service class to code the business logic and it acts as a middle layer between repository and controller class.
  • @Service annotation is a specialization of @Component annotation. Spring Service annotation can be applied only to classes. It is used to mark the class as a service provider.
  • @Transactional StateService annotate methods are executed in transactions.
    Example:
    @Transactional(readOnly = false, rollbackFor={Throwable.class, Exception.class}, propagation = Propagation.SUPPORTS)
  • @Autowired annotation is used for automatic dependency injection. Spring framework is built on dependency injection and we inject the class dependencies through spring bean configuration file.
StateService.java
Create Rest Controllers class. 
  • Create Rest StateController Controller class which contains all REST API endpoints for Create and Retrieve operations.
  • @RestController annotation was introduced in Spring 4.0 to simplify the creation of REST web services. It's a convenience annotation that combines @Controller and @ResponseBody– which eliminates the need to annotate every request handling method of the controller class with the @ResponseBody annotation.
StateService.java

Run the Project:
Write click to project and Run as->Spring boot App, it will check console to run project success and check running port.  



Testing using Postman.
Execute GET Request method to add a States to the Database. 

 

Video Link for watching How to build API.

Download Project: Download