发现一个很好用的全局异常处理
效果如下
代码
/**
* 处理一下常见的异常
**/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ResponseBody
@ExceptionHandler(MethodArgumentNotValidException.class)
public Response<String> exceptionHandler(MethodArgumentNotValidException e, NativeWebRequest request) {
FieldError fieldError = e.getBindingResult().getFieldError();
Response<String> error = Response.error(CommonErrorCode.E_999996.getCode(),
fieldError != null ? fieldError.getDefaultMessage() : e.getMessage());
outLog(e, request, error);
return error;
}
@ResponseBody
@ExceptionHandler(BindException.class)
public Response<String> exceptionHandler(BindException e, NativeWebRequest request) {
FieldError fieldError = e.getBindingResult().getFieldError();
Response<String> error = Response.error(CommonErrorCode.E_999996.getCode(),
fieldError != null ? fieldError.getDefaultMessage() : e.getMessage());
outLog(e, request, error);
return error;
}
@ResponseBody
@ExceptionHandler(HttpMessageNotReadableException.class)
public Response<String> exceptionHandler(HttpMessageNotReadableException e, NativeWebRequest request) {
Throwable rootCause = e.getRootCause();
Response<String> error = Response
.error(CommonErrorCode.CUSTOM.getCode(), rootCause != null ? rootCause.getMessage() : e.getMessage());
outLog(e, request, error);
return error;
}
@ResponseBody
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public Response<String> exceptionHandler(HttpMediaTypeNotSupportedException e, NativeWebRequest request) {
Response<String> error = Response.error(CommonErrorCode.CUSTOM.getCode(), e.getMessage());
outLog(e, request, error);
return error;
}
@ResponseBody
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public Response<String> exceptionHandler(HttpRequestMethodNotSupportedException e, NativeWebRequest request) {
Response<String> error = Response.error(CommonErrorCode.E_999995.getCode(), e.getMessage());
outLog(e, request, error);
return error;
}
@ResponseBody
@ExceptionHandler(PoolExhaustedException.class)
public Response<String> exceptionHandler(PoolExhaustedException e, NativeWebRequest request) {
Response<String> error = Response.error(CommonErrorCode.CUSTOM.getCode(), e.getMessage());
outLog(e, request, error);
return error;
}
private void outLog(Exception e, NativeWebRequest request, Response<String> error) {
HttpServletRequest httpRequest = request.getNativeRequest(HttpServletRequest.class);
String method = httpRequest != null ? httpRequest.getMethod() : null;
String url = httpRequest != null ? httpRequest.getRequestURI() : null;
String params = JSON.toJSONString(request.getParameterMap());
String json = JSON.toJSONString(error);
log.error("\n=======================================\n"
+ "GlobalExceptionHandler: {}\n"
+ "Method: {}\n"
+ "Url: {}\n"
+ "Params: {}\n"
+ "Response: {}\n"
+ "=======================================",
e.getMessage(), method, url, params, json, e);
}
}
解释
在Spring MVC中,@ExceptionHandler注解可以用于统一处理所有Controller中抛出的异常。这样我们就能够通过该注解统一进行异常处理,从而避免代码重复和冗余。
使用@ExceptionHandler注解非常简单,只需要在一个ControllerAdvice类中定义一个带有Exception参数的方法,并在该方法上标注@ExceptionHandler注解即可。当Controller中抛出异常时,Spring MVC会自动匹配对应的@ExceptionHandler方法来进行异常处理。
当然,在使用@ExceptionHandler注解时也有一些需要注意的事项:
- @ExceptionHandler注解只能处理Controller内部的异常,如果是Controller之外的异常则无法处理;
- @ExceptionHandler方法的参数必须为Exception或其子类,不能直接传入普通参数;
- 如果定义了多个@ExceptionHandler方法,并且它们可以匹配到同一种类型的异常,那么Spring MVC会使用距离抛出异常最近的方法进行异常处理;
- 可以在@ExceptionHandler方法中通过ModelAndView、ResponseEntity等方式返回自定义响应结果,而不仅限于ApiResponse等统一响应对象。
综上所述,使用@ExceptionHandler注解可以有效地统一处理Controller中抛出的异常,避免代码重复和冗余。但需要注意一些使用时的细节问题。

被折叠的 条评论
为什么被折叠?



