Design & Develop a Rest API with Spring Boot and MySQL (Part 1)
In this series of tutorials we will see the below contents
- How to design and develop a restful API for “User” entity with Spring Boot and MySQL (Part 1).
- How to handle exceptions in spring boot REST APIs (Part 2)
The programming languages, tools and frameworks that are required for developing Restful API are as follows.
- Java 8
- Maven 3
- Spring Data JPA
- Spring Web
- Lombok
- MySQL database
The design for the User Restful API routes are as below.
At the class level you should have a request mapping that consists of the name of the API you are developing and the version of the API as shown below.
@RequestMapping(“/userapi/v1”)
And at the method level you should have the following specification.
For write operations:
POST — /Users
PUT — /Users/{id}
DELETE — /Users/{id}
For Read operations:
GET — /Users (Retrieve all users)
GET — /Users/{id} (Retrieve a specific user)
Once you have the design, then we can start with the development of the User Restful API as shown below.
Create a spring boot project using the Spring Initializer website (https://start.spring.io/) as shown in the below screen shot
Click the GENERATE button and it will download the .zip file to your downloads directory. Then you should unzip to the appropriate folder of your choice.
Then import the project into the IDE of your choice. Here I am importing to the IntelliJ Idea as shown in the below screen shot.
The steps required to write a “User” Restful Service are as follows.
- Create the User Entity and annotate the same with JPA annotations
- Create a User Repository that extends from JPARepository
- Create a User Service
- Create a User Controller with Spring Boot annotations
Create the User Entity and annotate the same with JPA annotations
@Entity — Defines the user class as JPA entity
@Table — By default a table will be created with the same name as Entity class (user) and if you want to have a different name (users), then you should use this annotation
@Id — Defines the id field as primary key of the table
@GeneratedValue — Defines a strategy to generate the value for id field automatically by the database
@Column — By default the column name will be same as field name and if you want to have a different name to the column, then you should use this annotation
Create a User Repository that extends from JPARepository
JPARepository<User, Long> — Defines all the CRUD operations based on the generic types you specified like name of the entity class and type of the primary key
Create a User Service
You have to autowire UserRepository in the UserService class and can invoke the various CRUD operations that are available in the JPA repository.
Create a User Controller with Spring Boot annotations
Write Operations:
Read Operations:
In the UserController class, you have to autowire UserService class and can invoke the various operations that are available in that class.
The various annotations used in the Rest Controller are as below.
@RestController — Defines the UserController class as Restful API
@RequestMapping — This annotation maps HTTP requests to handler methods of REST controllers
@PostMapping — This annotation is used for mapping HTTP POST requests onto specific handler methods matched with given URI expression
@PutMapping — This annotation is used for mapping HTTP PUT requests onto specific handler methods matched with given URI expression
@DeleteMapping — This annotation is used for mapping HTTP DELETE requests onto specific handler methods matched with given URI expression
@GetMapping — This annotation is used for mapping HTTP GET requests onto specific handler methods matched with given URI expression
@RequestBody — This is used to convert the JSON user data to the User object
@PathVariable — This is used to map the dynamic parameters in the request path to the method arguments
For all the above CRUD operations we should return a ResponseEntity that is response body and response status code.
For that we can use either the ResponseEntity constructor or builder. In the above example code I have used builders to create and return ResponseEntity
Example for constructor:
return new ResponseEntity<>(user, HttpStatus.OK);
Example for builder:
return ResponseEntity.ok(user);
Testing the RESTful API using Postman
Create User API:
Update User API:
Delete User API:
Get User By ID:
Get All Users API:
That concludes how to create a RESTful API using Spring Boot Framework. If you like this tutorial please give a clap.