spring - 如何处理所有标准 Spring MVC 异常
问题描述
我已经实现了我的异常处理程序,我在其中捕获了我的自定义异常。现在,我只是想知道是否有一种很好的方法来捕获类中定义的所有异常:
org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
按照handleException方法的定义:
/**
* Provides handling for standard Spring MVC exceptions.
* @param ex the target exception
* @param request the current request
*/
@ExceptionHandler({
HttpRequestMethodNotSupportedException.class,
HttpMediaTypeNotSupportedException.class,
HttpMediaTypeNotAcceptableException.class,
MissingPathVariableException.class,
MissingServletRequestParameterException.class,
ServletRequestBindingException.class,
ConversionNotSupportedException.class,
TypeMismatchException.class,
HttpMessageNotReadableException.class,
HttpMessageNotWritableException.class,
MethodArgumentNotValidException.class,
MissingServletRequestPartException.class,
BindException.class,
NoHandlerFoundException.class,
AsyncRequestTimeoutException.class
})
@Nullable
public final ResponseEntity<Object> handleException(Exception ex, WebRequest request) throws Exception {
...
}
所以基本上我希望我ExceptionHandler
能够捕获上面列出的所有异常。最好避免覆盖每一个句柄方法。我需要这个才能将带有状态、errorMessage、时间戳、ecc 的经典 json 发送回客户端。
解决方案
推荐阅读
- python - 在 Win32Com 中检索 Receipints 电子邮件地址
- python - 为分类数据实施 K 模式聚类
- java - 在 Java 中使用正则表达式来识别相同的字符串
- windows - 为 Windows 安装第三方 POSIX 线程
- css - 您如何在@font-face 规则中包含替代项?
- json - 如何将嵌套的逗号分隔 JSON 数组中的元素加载到 SQL 表中
- unity3d - 为什么新资产不从资源文件夹加载?
- ionic-framework - Ionic 5 使用 facebook 登录,它只适用于我的 facebook-email
- sql-server - 使用 xquery 从 XML 中检索多个分隔值
- blazor - 在 Blazor 中修改菜单间距