休息暴露的spring-data的全局异常处理

时间:2015-03-29 16:35:57

标签: spring-boot spring-data-rest

使用spring-data-rest来公开我想要覆盖默认异常处理的存储库。

阅读文档,我认为最好的wat将使用@ControllerAdvice注释类

@ControllerAdvice
class GlobalControllerExceptionHandler extends ResponseEntityExceptionHandler {

    Logger log = LoggerFactory.getLogger(GlobalControllerExceptionHandler.class);

    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Object> badRequest(HttpServletRequest req, Exception exception) {
        log.info("++++ GLOBAL EXCEPTION HANDLING ++++");
        return null;
    }
}

有几点我不确定:

  • 我应该扩展ResponseEntityExceptionHandler,其他类,什么都没有?
  • 我应该返回ResponseEntity对象,其他类吗?

顺便说一下,即使我尝试了不同的配置,这似乎也不起作用。有没有办法在spring-data-rest中自定义错误处理?

1 个答案:

答案 0 :(得分:0)

它缺少RepositoryRestExceptionHandler。会是这样的:

像这样:

@ControllerAdvice(basePackageClasses = RepositoryRestExceptionHandler.class)
public class GlobalControllerExceptionHandler {

    Logger log = LoggerFactory.getLogger(GlobalControllerExceptionHandler.class);

    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Object> badRequest(HttpServletRequest req, Exception exception) {
        log.info("++++ GLOBAL EXCEPTION HANDLING ++++");
        return null;
    }
}