休息暴露的弹簧数据的全局异常处理

休息暴露的弹簧数据的全局异常处理

本文介绍了休息暴露的弹簧数据的全局异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using spring-data-rest to expose repositories i want to overwrite the default exception handling.

阅读文档,我认为最好的方法是使用 @ControllerAdvice 注释类

reading documentation it looks to me that the best wat would be using a @ControllerAdvice annotated class

@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 有没有自定义错误处理的方法?

By the way this does not seems to work even when i've tried different configurations. Is there a way to customize error handling in spring-data-rest?

推荐答案

缺少 RepositoryRestExceptionHandler.应该是这样的:

It's missing RepositoryRestExceptionHandler. Would be something like this:

像这样:

@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;
    }
}

这篇关于休息暴露的弹簧数据的全局异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:29