filemonczyk filemonczyk - 2 months ago 8
Java Question

Accessing the error object inside of exceptionhandler method

Is there any way to get inside to the exception object when using annotation @ExceptionHandler?

this is my code:

@ExceptionHandler(DataInputException.class)
public ResponseEntity handleException(){
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body("Entity contains null or forbidden values");
}


I'd like to have returned message to contain customized info about perticular fields. (That's why I need error object).

Answer

Pass in the exception

@ExceptionHandler(DataInputException.class)
public ResponseEntity handleException(DataInputException exception) {