Spring Boot项目——统一异常处理
背景
- 在做项目时,会产生各种各样业务异常,大致可以分为下面几类
- 参数异常:服务端接收客户端参数时,参数不符合规则而产生的问题
- 数据库异常:服务端和数据库交互时发生的异常
- 业务异常:系统业务产生的问题
- 未知异常:不属于以上三种异常的情况
解决思路
- 对这些项目异常进行分类编码,服务端需要将这些异常信息返回给客户端。
- 建立全局异常处理类,在类中针对不同类型异常进行返回值处理,包括默认返回结果。
- 在业务中,不同未知抛出不同异常
关键代码
- 全局异常处理
@RestControllerAdvice @Slf4j public class GlobalExceptionHandler { /** *业务异常 */ @ExceptionHandler(BusinessException.class) public RetResult businessExceptionHandler(HttpServletRequest request, BusinessException e){ if(Objects.nonNull(request)){ StringBuffer requestUrlBuffer = request.getRequestURL(); String method = request.getMethod(); String requestUrl = Objects.isNull(requestUrlBuffer) ? request.getRequestURI() : requestUrlBuffer.toString(); log.info("BusinessException:{}, \n请求URL: [{}], \n请求方式: [{}]\n", e.getMessage(), requestUrl, method, e); } return ResultUtil.error(e.getCode(), e.getMessage()); } /** *参数异常 */ @ExceptionHandler(ParamException.class) public RetResult paramExceptionHandler(HttpServletRequest request, ParamException e) { if (Objects.nonNull(request)) { StringBuffer requestUrlBuffer = request.getRequestURL(); String method = request.getMethod(); String requestUrl = Objects.isNull(requestUrlBuffer) ? request.getRequestURI() : requestUrlBuffer.toString(); log.info("ParamException:{}, \n请求URL: [{}], \n请求方式: [{}]\n", e.getMessage(), requestUrl, method, e); } return ResultUtil.error(e.getCode(), e.getMessage()); } /** *数据库异常 */ @ExceptionHandler(DataBaseException.class) public RetResult databseExceptionHandler(HttpServletRequest request, DataBaseException e) { if (Objects.nonNull(request)) { StringBuffer requestUrlBuffer = request.getRequestURL(); String method = request.getMethod(); String requestUrl = Objects.isNull(requestUrlBuffer) ? request.getRequestURI() : requestUrlBuffer.toString(); log.info("DatabseExceptionHandler{}, \n请求URL: [{}], \n请求方式: [{}]\n", e.getMessage(), requestUrl, method, e); } return ResultUtil.error(e.getCode(), e.getMessage()); } @ExceptionHandler(RuntimeException.class) public RetResult runtimeExceptionHandler(HttpServletRequest request, RuntimeException e){ if(Objects.nonNull(request)){ StringBuffer requestUrlBuffer = request.getRequestURL(); String method = request.getMethod(); String requestUrl = Objects.isNull(requestUrlBuffer) ? request.getRequestURI() : requestUrlBuffer.toString(); log.error("RuntimeException:{}, \n请求URL: [{}], \n请求方式: [{}]\n", e.getMessage(), requestUrl, method, e); } return ResultUtil.error(EnumResultCode.UN_KNOW_ERROR.getCode(), e.getMessage()); } @ExceptionHandler(Exception.class) public RetResult exceptionHandler(HttpServletRequest request, Exception e){ if(Objects.nonNull(request)){ StringBuffer requestUrlBuffer = request.getRequestURL(); String method = request.getMethod(); String requestUrl = Objects.isNull(requestUrlBuffer) ? request.getRequestURI() : requestUrlBuffer.toString(); log.error("Exception:{}, \n请求URL: [{}], \n请求方式: [{}]\n", e.getMessage(), requestUrl, method, e); } return ResultUtil.error(EnumResultCode.SYSTEM_ERROR.getCode(), e.getMessage()); } }
代码下载
- 源代码可点击exce.zip下载