java 异常统一管理_Java架构之路- 统一异常处理

在web应用中请求处理时,出现异常是非常常见的。所以当应用出现各类异常时,进行异常的捕获和处理非常重要的,本文重点讲解一下统一异常和数据校验

利用@ControllerAdvice和@ExceptionHandler定义一个统一异常处理类

@ControllerAdvice:控制器增强,使@ExceptionHandler、@InitBinder、@ModelAttribute注解的方法应用到所有的 @RequestMapping注解的方法。

@ExceptionHandler:异常处理器,此注解的作用是当出现其定义的异常时进行处理的方法

通用业务异常

/**

* 通用业务异常

*

*/

@NoArgsConstructor

public class BizException extends RuntimeException {

public BizException(String message) {

super(message);

}

public BizException(String message, Throwable cause) {

super(message, cause);

}

public BizException(Throwable cause) {

super(cause);

}

}

提示异常

/**

* 仅用于提示的异常,该异常不会导致输出堆栈

*

*/

@NoArgsConstructor

public class TipException extends RuntimeException {

public TipException(String message) {

super(message);

}

}

全局异常

/**

* 全局异常拦截,主要拦截验证信息

*

*/

@Slf4j

@ControllerAdvice

public class GlobalExceptionHandler {

/**

* 参数校验失败异常

*

* @param e

* @return

*/

@ExceptionHandler(value = BindException.class)

@ResponseBody

public BaseResponse handleBindException(BindException e) {

return buildErrorResponse(e, e.getBindingResult());

}

@ExceptionHandler(value = MethodArgumentNotValidException.class)

@ResponseBody

public BaseResponse handleBindException(MethodArgumentNotValidException e) {

return buildErrorResponse(e, e.getBindingResult());

}

@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)

@ResponseBody

public BaseResponse handleHttpMethodException(HttpRequestMethodNotSupportedException e) {

String msg = String.format("不支持 [%s] 请求", e.getMethod());

return Response.fail(msg).build();

}

@ExceptionHandler(value = MissingServletRequestParameterException.class)

@ResponseBody

public BaseResponse handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {

String msg = String.format("缺少 %s 参数", e.getParameterName());

return Response.fail(msg).build();

}

@ExceptionHandler(value = BizException.class)

@ResponseBody

public BaseResponse handleBizException(BizException e) {

return Response.fail(e.getMessage()).build();

}

@ExceptionHandler(value = TipException.class)

@ResponseBody

public BaseResponse handleTipException(TipException e) {

return Response.fail(e.getMessage()).build();

}

@ExceptionHandler(value = MultipartException.class)

@ResponseBody

public BaseResponse handleMultipartException(MultipartException e) {

return Response.fail(e.getCause().getMessage()).build();

}

@ExceptionHandler(value = MissingServletRequestPartException.class)

@ResponseBody

public BaseResponse handleMissingServletRequestPartException(MissingServletRequestPartException e) {

return Response.fail("请上传文件").build();

}

private BaseResponse buildErrorResponse(Exception e, BindingResult bindingResult) {

if (null != bindingResult && bindingResult.hasFieldErrors()) {

return Response.fail(bindingResult.getFieldError().getDefaultMessage()).build();

}

log.warn("参数校验异常: ", e);

return Response.fail("参数校验失败").build();

}

/**

* 全局异常

*

* @param e

* @return

*/

@ExceptionHandler(value = Exception.class)

@ResponseBody

public BaseResponse handleException(Exception e) {

log.error(e.getMessage(), e);

return Response.fail("请求失败").build();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值