I want to override html error page for 404 responses as an JSON response. When i use @ControllerAdvice
without @EnableWebMvc
it is not working.
@EnableWebMvc // if i remove this, it is not working
@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GlobalControllerExceptionHandler {
@ExceptionHandler(NoHandlerFoundException.class)
public ResponseEntity<ErrorDTO> noHandlerFoundException(
HttpServletRequest request,
NoHandlerFoundException exception) {
ErrorDTO errorDTO = new ErrorDTO();
return new ResponseEntity<>(errorDTO, HttpStatus.NOT_FOUND);
}
}
Is there an option for custom exception handling without @EnableWebMvc
, because it overrides Spring configurations which are declared inside application.yml.