diff --git a/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/controller/StudentController.java b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/controller/StudentController.java index 75920f8..9804ef9 100644 --- a/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/controller/StudentController.java +++ b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/controller/StudentController.java @@ -2,6 +2,8 @@ import java.util.List; +import com.wasim.rest.api.request.StudentRequest; +import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -25,9 +27,18 @@ public class StudentController { private StudentService studentService; @PostMapping("/saveStudent") - public ResponseEntity createStudent(@RequestBody Student student) { - - return new ResponseEntity(studentService.createStudent(student), HttpStatus.CREATED); + public ResponseEntity createStudent(@Valid @RequestBody StudentRequest request) { + + // Here Converting the Request to Entity. + Student student = new Student(); + student.setName(request.getName()); + student.setEmail(request.getEmail()); + + //called service + Student savedStudent = studentService.createStudent(student); + + //returning the response + return new ResponseEntity<>(savedStudent, HttpStatus.CREATED); } @GetMapping("/getAllStudent") diff --git a/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/exception/GlobalExceptionHandler.java b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..1a6c617 --- /dev/null +++ b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/exception/GlobalExceptionHandler.java @@ -0,0 +1,31 @@ +package com.wasim.rest.api.exception; + +import java.util.HashMap; +import java.util.Map; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice(basePackages = "com.wasim.rest.api") +public class GlobalExceptionHandler { + + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity> handleValidationExceptions(MethodArgumentNotValidException ex) { + + System.out.println("GlobalExceptionHandler hit"); + Map errors = new HashMap<>(); + + ex.getBindingResult().getFieldErrors().forEach(error -> + errors.put(error.getField(), error.getDefaultMessage()) + ); + + Map response = new HashMap<>(); + response.put("message", "Validation failed"); + response.put("errors", errors); + + return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); + } +} \ No newline at end of file diff --git a/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/request/StudentRequest.java b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/request/StudentRequest.java new file mode 100644 index 0000000..b0595d6 --- /dev/null +++ b/Spring-Boot-REST-API/src/main/java/com/wasim/rest/api/request/StudentRequest.java @@ -0,0 +1,30 @@ +package com.wasim.rest.api.request; + +import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.NotBlank; + +public class StudentRequest { + + @NotBlank(message = "Name is required") + private String name; + + @NotBlank(message = "Email is required") + @Email(message = "Invalid email format") + private String email; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} \ No newline at end of file