I try to intercept custom exception with @ControllerAdvice annotation.
This is code:
@ControllerAdvice(basePackages = "{com.ciro.cotroller}")
@RestController
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = {TodoNotFoundException.class})
public final ResponseEntity<ExceptionResponse> todoNotFoundException(TodoNotFoundException exception){
ExceptionResponse exceptionResponse = new ExceptionResponse(exception.getMessage(), "custom details");
return new ResponseEntity<ExceptionResponse>(exceptionResponse,HttpStatus.NOT_FOUND);
}
}
This is my exception:
package com.ciro.exception;
public class TodoNotFoundException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
public TodoNotFoundException() {
throw new RuntimeException("An custom error is raised!");
}
}
But default entity response is returned.