SpringBoot 全局统一异常拦截器

@ControllerAdvice
@ResponseBody
public class GlobalExceptionInterceptor {
@ExceptionHandler(value = Exception.class)
public String exceptionHandler(HttpServletRequest request, Exception e ) {
String failMsg = null;
if (e instanceof MethodArgumentNotValidException) {
// 拿到参数校验具体异常信息提示
failMsg = ((MethodArgumentNotValidException)e).getBindingResult().getFieldError().getDefaultMessage();
}
// 直接吐回给前端
return failMsg;
}
}
posted @ 2021-11-11 15:32  Bob.Xie  阅读(320)  评论(0编辑  收藏  举报