Introduction
In this chapter, we will build a save Student REST API for our Student Management System Project with a MySQL database. We will create a service layer with an interface and implementation class, build the save Student REST API, and test it using the Postman client. Each step will be explained in detail. In the next chapter, we will build a Get Single Student REST API.
What We Will Cover in This Chapter
- Create a Service Layer (interface and implementation class)
- Build Save Student REST API
- Test using Postman client
- Conclusion
Create a Service Layer
Create a Service Interface
-
Create a New Package:
- In the
src/main/java/com/example/springbootmysqlcrud
directory, create a new package namedservice
.
- In the
-
Create the
StudentService
Interface:- Inside the
service
package, create a new interface namedStudentService
. - Add the following code to the
StudentService
interface:
- Inside the
package com.example.springbootmysqlcrud.service; import com.example.springbootmysqlcrud.model.Student; public interface StudentService { Student saveStudent(Student student); }
Explanation
- Service Interface: Defines the contract for the service layer. It includes the
saveStudent
method to save a student.
Create a Service Implementation Class
- Create the
StudentServiceImpl
Class:- Inside the
service
package, create a new class namedStudentServiceImpl
. - Add the following code to the
StudentServiceImpl
class:
- Inside the
package com.example.springbootmysqlcrud.service; import com.example.springbootmysqlcrud.model.Student; import com.example.springbootmysqlcrud.repository.StudentRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class StudentServiceImpl implements StudentService { @Autowired private StudentRepository studentRepository; @Override public Student saveStudent(Student student) { return studentRepository.save(student); } }
Explanation
@Service
: Indicates that this class is a service component in the Spring context.StudentRepository
: TheStudentRepository
instance is injected into the service class to interact with the database.saveStudent
Method: Implements thesaveStudent
method defined in theStudentService
interface to save a student entity.
Build Save Student REST API
Create a Controller Class
-
Create a New Package:
- In the
src/main/java/com/example/springbootmysqlcrud
directory, create a new package namedcontroller
.
- In the
-
Create the
StudentController
Class:- Inside the
controller
package, create a new class namedStudentController
. - Add the following code to the
StudentController
class:
- Inside the
package com.example.springbootmysqlcrud.controller; import com.example.springbootmysqlcrud.model.Student; import com.example.springbootmysqlcrud.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/api/students") public class StudentController { @Autowired private StudentService studentService; @PostMapping public ResponseEntity<Student> saveStudent(@RequestBody Student student) { Student savedStudent = studentService.saveStudent(student); return new ResponseEntity<>(savedStudent, HttpStatus.CREATED); } }
Explanation
@RestController
: Indicates that this class is a REST controller.@RequestMapping("/api/students")
: Maps HTTP requests to/api/students
to methods in this controller.@PostMapping
: Handles HTTP POST requests to save a student.ResponseEntity
: Wraps the response and sets the HTTP status to201 Created
for successful creation.
Test Using Postman Client
Step-by-Step Testing
-
Open Postman:
- Launch the Postman client from your installed applications.
-
Create a New Request:
- Click on "New" and then select "Request".
-
Set Request Type and URL:
- Set the request type to
POST
. - Enter the URL:
http://localhost:8080/api/students
.
- Set the request type to
-
Set Request Body:
- Click on the "Body" tab.
- Select "raw" and "JSON".
- Enter the following JSON data:
{ "firstName": "Amit", "lastName": "Sharma", "email": "amit.sharma@example.com" }
- Send Request:
- Click the "Send" button.
- Verify that the response status is
201 Created
and the response body contains the saved student details.
Explanation
- Request Type:
POST
indicates that we are sending data to create a new resource. - URL: Points to the save Student REST API endpoint.
- Request Body: Contains the student details to be saved in JSON format.
- Response: Verifies that the student has been successfully saved.
Conclusion
In this chapter, we built a save Student REST API for our Student Management System Project with a MySQL database. We created a service layer with an interface and implementation class, built the save Student REST API, and tested it using the Postman client. Each step was explained in detail. In the next chapter, we will build a Get Single Student REST API.