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/springbootmysqlcruddirectory, create a new package namedservice.
- In the
-
Create the
StudentServiceInterface:- Inside the
servicepackage, create a new interface namedStudentService. - Add the following code to the
StudentServiceinterface:
- 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
saveStudentmethod to save a student.
Create a Service Implementation Class
- Create the
StudentServiceImplClass:- Inside the
servicepackage, create a new class namedStudentServiceImpl. - Add the following code to the
StudentServiceImplclass:
- 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: TheStudentRepositoryinstance is injected into the service class to interact with the database.saveStudentMethod: Implements thesaveStudentmethod defined in theStudentServiceinterface 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/springbootmysqlcruddirectory, create a new package namedcontroller.
- In the
-
Create the
StudentControllerClass:- Inside the
controllerpackage, create a new class namedStudentController. - Add the following code to the
StudentControllerclass:
- 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/studentsto methods in this controller.@PostMapping: Handles HTTP POST requests to save a student.ResponseEntity: Wraps the response and sets the HTTP status to201 Createdfor 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 Createdand the response body contains the saved student details.
Explanation
- Request Type:
POSTindicates 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.