Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 在ResponseEntityExceptionHandler中,handleExceptionInternal和handleException之间的确切区别是什么?_Spring_Spring Boot_Spring Mvc_Exception - Fatal编程技术网

Spring 在ResponseEntityExceptionHandler中,handleExceptionInternal和handleException之间的确切区别是什么?

Spring 在ResponseEntityExceptionHandler中,handleExceptionInternal和handleException之间的确切区别是什么?,spring,spring-boot,spring-mvc,exception,Spring,Spring Boot,Spring Mvc,Exception,我正在实现ResponseEntityExceptionHandler的所有方法,因为我不希望Spring向客户端发送任何标准错误响应。有两种看似相似的方法让我有点困惑。即handleException内部和handleException。这是两种方法的定义 我觉得这些解释有点模糊。例如,什么可以被视为“标准spring mvc例外”?是否应该将handleExceptionInternal视为“默认”处理程序方法,在其他方法都无法捕获spring异常时使用?如果我错了,请纠正我 谢谢handl

我正在实现
ResponseEntityExceptionHandler
的所有方法,因为我不希望Spring向客户端发送任何标准错误响应。有两种看似相似的方法让我有点困惑。即
handleException内部
handleException
。这是两种方法的定义

我觉得这些解释有点模糊。例如,什么可以被视为“标准spring mvc例外”?是否应该将
handleExceptionInternal
视为“默认”处理程序方法,在其他方法都无法捕获spring异常时使用?如果我错了,请纠正我


谢谢

handleException方法是标准spring mvc异常的常见异常处理程序。它的主要任务是根据http响应代码约定将这些异常映射到相应的状态代码,您很可能不会更改这些状态代码。 e、 g

所有这些异常都在其特定的处理程序方法
handle{ExceptionName}
中处理,因此,出于某种原因,如果您想更改状态代码(或添加响应正文以获取详细信息),可以通过重写特定的处理程序来实现。所有这些处理程序进一步委托给
handleExceptionInternal

您注意到,每个
handle{ExceptionName}
方法将
body
作为null传递给
handleExceptionInternal
。这些方法只返回没有正文的状态代码,不会提供有关错误的更多详细信息

通常的做法是返回带有详细信息的自定义错误响应主体,以便api使用者知道确切的错误原因。通过创建
Error
对象,可以在此处注入自定义实体。一条简单的错误消息如下所示

public class ApiError {

    private final int status;
    private final int message;

    public ApiError(int status, int message) {
        this.status = status;
        this.message = message;
    }
    // getters
}
您可以重写
handleExceptionInternal
方法,如下所示:

@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
    ApiError error = new ApiError(status.value(), ex.getMessage());
    return super.handleExceptionInternal(ex, error, headers, status, request);
}
@覆盖
受保护的ResponseEntity handleExceptionInternal(异常ex、对象体、HttpHeaders、HttpStatus状态、WebRequest请求){
APIRERROR error=新的APIRROR(status.value(),例如getMessage());
返回super.handleExceptionInternal(例如,错误、标题、状态、请求);
}
摘要
如果
handleException
不存在,则需要手动将每个异常映射到相应的错误代码。如果缺少
handleExceptionInternal
,则要插入错误体,需要重写每个
handle{Exception}
方法

更新
用于http状态代码定义

public class ApiError {

    private final int status;
    private final int message;

    public ApiError(int status, int message) {
        this.status = status;
        this.message = message;
    }
    // getters
}
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
    ApiError error = new ApiError(status.value(), ex.getMessage());
    return super.handleExceptionInternal(ex, error, headers, status, request);
}