更新时间:2022-06-12 00:26:57
您可以使用 @ControllerAdvice
批注创建全局异常处理.像这样:
You can create global exception handling with @ControllerAdvice
annotation. Like this:
@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = YourExceptionTypes.class)
protected ResponseEntity<Object> handleBusinessException(RuntimeException exception, WebRequest request) {
return handleExceptionInternal(exception, exception.getMessage(), new HttpHeaders(), HttpStatus.NOT_ACCEPTABLE, request);
}
}
引发异常时,处理程序将捕获该异常并将其转换为所需的响应.原始的异常不会传播.
When an exception is thrown, the handler will catch and transform it to the desired response. The original exception wont be propagated.