代码之家  ›  专栏  ›  技术社区  ›  Dhruv Kapatel

在带有Spring Rest的全局异常处理程序中使用通用异常类处理程序是一个好的实践吗?

  •  2
  • Dhruv Kapatel  · 技术社区  · 7 年前

    我引用了一些文章来创建全局异常处理程序 @ControllerAdvice 对于我的RESTAPI项目,使用Spring。这样做的目的是在发生异常时向客户机发送正确的格式化响应。在他们添加的一些文章中 Throwable Exception 在全局异常处理程序中。 我要换它吗 RunTimeException 因为此块用于运行时发生异常?

    异常处理程序代码:

    @ControllerAdvice
    public class GlobalExceptionHandler{
    
        @ExceptionHandler(NoDataFoundException.class)
        @ResponseStatus(code=HttpStatus.NOT_FOUND)
        public ResponseEntity<ErrorResponse> handle(NoDataFoundException ex){
            ErrorResponse errorResponse = new ErrorResponse(ex.getMessage(), HttpStatus.NOT_FOUND.value());
            ResponseEntity<ErrorResponse> response = new ResponseEntity<ErrorResponse>(errorResponse, HttpStatus.NOT_FOUND);
            return response;
        }
        ..... more methods to handle custom exceptions
    
        @ExceptionHandler(Exception.class)
        @ResponseStatus(code=HttpStatus.INTERNAL_SERVER_ERROR)
        public ResponseEntity<ErrorResponse> handle(Exception ex){
            ErrorResponse errorResponse = new ErrorResponse("Something went wrong", HttpStatus.INTERNAL_SERVER_ERROR.value());
            ResponseEntity<ErrorResponse> response = new ResponseEntity<ErrorResponse>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
            return response;
        }
    }
    

    错误响应代码:

    public class ErrorResponse {
    
        private String message;
        private int statusCode;
    
        public ErrorResponse(String message, int statusCode) {
            super();
            this.message = message;
            this.statusCode = statusCode;
        }
        public String getMessage() {
            return message;
        }
        public void setMessage(String message) {
            this.message = message;
        }
        public int getStatusCode() {
            return statusCode;
        }
        public void setStatusCode(int statusCode) {
            this.statusCode = statusCode;
        }
    }  
    

    参考文献:

    1. https://dzone.com/articles/exception-handling-in-spring-boot-rest-web-service
    2. https://github.com/in28minutes/spring-boot-examples/tree/master/spring-boot-2-rest-service-exception-handling
    2 回复  |  直到 7 年前
        1
  •  1
  •   davidxxx    7 年前

    我应该用runtimeexception替换它,就像这个块用于 运行时发生异常?

    以确保捕获任何由组件引发且从未由其处理的异常,或捕获任何类型化异常超过的异常处理程序。 Exception ,您应该有一个 例外 .
    处理程序 RuntimeException 是不够的,因为在运行时,如果高级组件的方法声明指定了 throws Exception throws "any checked exception" ,可以将选中的异常传播到客户端或在此处应用默认行为的容器。
    例如,假设这个REST控制器方法声明可能导致这种情况发生:

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<Foo> getOne(@PathVariable long id) throws Exception {
           // ....           
    }
    

    为了覆盖这个默认的Spring行为,您需要为 例外 .
    当然,这并不意味着只为 例外 是这样的,但是您可能有一些例外,没有特定的处理,因此一般的处理是可以的。

        2
  •  0
  •   Makoto    7 年前

    老实说,有一个异常处理程序句柄 Exception 在我看来有点懒惰。自从 例外 如果选中,您应该负责处理或从错误中恢复。如果你是 能够从错误中恢复,或者您所处的环境阻止您编写允许您优雅地恢复的代码,应该将其重新放入 RuntimeException 而是指出问题所在。

    当然,异常处理程序有两个用途:

    • 它们为您提供了定义错误响应外观和详细内容的标准的能力。
    • 它们使您能够记录错误,以便稍后返回并修复错误。

    我强烈建议重新检查投掷的模式 例外 不选中,并在异常处理程序中处理这些异常。作为一个防故障的全部捕获,您可以使用一个通用的异常处理程序 例外 捕获所有未转换的点,并记录发生的情况。

    它应该 从未 如果你作为一个开发者允许 例外 一路传播到顶端,没有明确的原因。