Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,9 +27,18 @@ public class StudentController {
private StudentService studentService;

@PostMapping("/saveStudent")
public ResponseEntity<Student> createStudent(@RequestBody Student student) {

return new ResponseEntity<Student>(studentService.createStudent(student), HttpStatus.CREATED);
public ResponseEntity<Student> 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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Map<String, Object>> handleValidationExceptions(MethodArgumentNotValidException ex) {

System.out.println("GlobalExceptionHandler hit");
Map<String, String> errors = new HashMap<>();

ex.getBindingResult().getFieldErrors().forEach(error ->
errors.put(error.getField(), error.getDefaultMessage())
);

Map<String, Object> response = new HashMap<>();
response.put("message", "Validation failed");
response.put("errors", errors);

return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}