Rest Exception Handling & Rest Global Exception Handling

 Rest Exception Handling & Rest Global Exception Handling:


Rest Exception Handling:

StudentErrorResponse.java

package com.luv2code.demo.rest;

public class StudentErrorResponse {

private int status;
private String message;
private long timeStamp;

public StudentErrorResponse() {

}

public StudentErrorResponse(int status, String message, long timeStamp) {
this.status = status;
this.message = message;
this.timeStamp = timeStamp;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public long getTimeStamp() {
return timeStamp;
}

public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}
}

StudentNotFoundException.java

package com.luv2code.demo.rest;

public class StudentNotFoundException extends RuntimeException {

public StudentNotFoundException(String message) {
super(message);
}

public StudentNotFoundException(String message, Throwable cause) {
super(message, cause);
}

public StudentNotFoundException(Throwable cause) {
super(cause);
}
}
StudentRestController.java
package com.luv2code.demo.rest;

import com.luv2code.demo.entity.Student;
import jakarta.annotation.PostConstruct;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
public class StudentRestController {

private List<Student> theStudents;

// define @PostConstruct to load the student data ... only once!

@PostConstruct
public void loadData() {

theStudents = new ArrayList<>();

theStudents.add(new Student("Poornima", "Patel"));
theStudents.add(new Student("Mario", "Rossi"));
theStudents.add(new Student("Mary", "Smith"));
}


// define endpoint for "/students" - return a list of students

@GetMapping("/students")
public List<Student> getStudents() {

return theStudents;
}

// define endpoint or "/students/{studentId}" - return student at index

@GetMapping("/students/{studentId}")
public Student getStudent(@PathVariable int studentId) {

// just index into the list ... keep it simple for now

// check the studentId again list size

if ( (studentId >= theStudents.size()) || (studentId < 0)) {
throw new StudentNotFoundException("Student id not found - " + studentId);
}

return theStudents.get(studentId);
}
//
// Add an exception handler using @ExceptionHandler
//


// Controller-Level Scope
// The annotated method will only handle exceptions thrown by the controller methods in the same class.

@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {

// create a StudentErrorResponse

StudentErrorResponse error = new StudentErrorResponse();

error.setStatus(HttpStatus.NOT_FOUND.value()); //404
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());

// return ResponseEntity

return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
}

// add another exception handler ... to catch any exception (catch all)

@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(Exception exc) {

// create a StudentErrorResponse
StudentErrorResponse error = new StudentErrorResponse();

error.setStatus(HttpStatus.BAD_REQUEST.value()); //400
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());

// return ResponseEntity
return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
}
}




Rest Global Exception Handling:

StudentErrorResponse.java

package com.luv2code.demo.rest;

public class StudentErrorResponse {

private int status;
private String message;
private long timeStamp;

public StudentErrorResponse() {

}

public StudentErrorResponse(int status, String message, long timeStamp) {
this.status = status;
this.message = message;
this.timeStamp = timeStamp;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public long getTimeStamp() {
return timeStamp;
}

public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}
}

StudentNotFoundException.java

package com.luv2code.demo.rest;

public class StudentNotFoundException extends RuntimeException {

public StudentNotFoundException(String message) {
super(message);
}

public StudentNotFoundException(String message, Throwable cause) {
super(message, cause);
}

public StudentNotFoundException(Throwable cause) {
super(cause);
}
}

StudentRestController.java

package com.luv2code.demo.rest;

import com.luv2code.demo.entity.Student;
import jakarta.annotation.PostConstruct;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
public class StudentRestController {

private List<Student> theStudents;

// define @PostConstruct to load the student data ... only once!

@PostConstruct
public void loadData() {

theStudents = new ArrayList<>();

theStudents.add(new Student("Poornima", "Patel"));
theStudents.add(new Student("Mario", "Rossi"));
theStudents.add(new Student("Mary", "Smith"));
}


// define endpoint for "/students" - return a list of students

@GetMapping("/students")
public List<Student> getStudents() {

return theStudents;
}

// define endpoint or "/students/{studentId}" - return student at index

@GetMapping("/students/{studentId}")
public Student getStudent(@PathVariable int studentId) {

// just index into the list ... keep it simple for now

// check the studentId again list size

if ( (studentId >= theStudents.size()) || (studentId < 0)) {
throw new StudentNotFoundException("Student id not found - " + studentId);
}

return theStudents.get(studentId);
}
}

StudentRestExceptionHandler.java

package com.luv2code.demo.rest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;


// Global Scope Exception Handler
@ControllerAdvice
public class StudentRestExceptionHandler {

// add exception handling code here

@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {

// create a StudentErrorResponse

StudentErrorResponse error = new StudentErrorResponse();

error.setStatus(HttpStatus.NOT_FOUND.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());

// return ResponseEntity

return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
}

// add another exception handler ... to catch any exception (catch all)

@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(Exception exc) {

// create a StudentErrorResponse
StudentErrorResponse error = new StudentErrorResponse();

error.setStatus(HttpStatus.BAD_REQUEST.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());

// return ResponseEntity
return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);
}
}

💫The End 💫

Comments