API Exception Handler
To handle the exceptio, we use @ExceptionHandler(Exception type)
For example:
@ExceptionHandler(org.springframework.http.converter.HttpMessageNotReadableException.class)
@ResponseBody
public ResponseEntity<Object> handleValidationException(org.springframework.http.converter.HttpMessageNotReadableException ex) {
return ResponseEntity.status(400).body(new Object() {
public final String error = "Bad Request for greet";
public final String message = ex.getMessage();
});
}
In this case, whenever we have a HttpMessageNotReadableException
, we will call the below method which will return a JSON of
{
error: "Bad Request for greet"
message: "Exception message..."
}
with status code 400