Build Save Student REST API

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

  1. Create a New Package:

    • In the src/main/java/com/example/springbootmysqlcrud directory, create a new package named service.
  2. Create the StudentService Interface:

    • Inside the service package, create a new interface named StudentService.
    • Add the following code to the StudentService interface:
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

  1. Create the StudentServiceImpl Class:
    • Inside the service package, create a new class named StudentServiceImpl.
    • Add the following code to the StudentServiceImpl class:
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: The StudentRepository instance is injected into the service class to interact with the database.
  • saveStudent Method: Implements the saveStudent method defined in the StudentService interface to save a student entity.

Build Save Student REST API

Create a Controller Class

  1. Create a New Package:

    • In the src/main/java/com/example/springbootmysqlcrud directory, create a new package named controller.
  2. Create the StudentController Class:

    • Inside the controller package, create a new class named StudentController.
    • Add the following code to the StudentController class:
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 to 201 Created for successful creation.

Test Using Postman Client

Step-by-Step Testing

  1. Open Postman:

    • Launch the Postman client from your installed applications.
  2. Create a New Request:

    • Click on "New" and then select "Request".
  3. Set Request Type and URL:

    • Set the request type to POST.
    • Enter the URL: http://localhost:8080/api/students.
  4. 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" } 
  1. 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.

Leave a Comment

Scroll to Top