【发布时间】:2018-08-16 10:44:36
【问题描述】:
我想将 404 响应的 html 错误页面覆盖为 JSON 响应。当我在没有@EnableWebMvc 的情况下使用@ControllerAdvice 时,它不起作用。
@EnableWebMvc // if i remove this, it is not working
@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GlobalControllerExceptionHandler {
@ExceptionHandler(NoHandlerFoundException.class)
public ResponseEntity<ZeusErrorDTO> noHandlerFoundException(
HttpServletRequest request,
NoHandlerFoundException exception) {
ErrorDTO errorDTO = new ErrorDTO();
return new ResponseEntity<>(errorDTO, HttpStatus.NOT_FOUND);
}
}
是否有不带@EnableWebMvc 的自定义异常处理选项,因为它会覆盖在 application.yml 中声明的 Spring 配置。
【问题讨论】:
标签: java spring-boot