企业实战之spring增强器实现《全局异常处理器》

前言

之前我们分享了对于spring项目的controller层,我们该如何简写我们的代码逻辑,所谓的简写就是 简化日志打印、参数校验、异常捕获和响应结果的封装这几个步骤,让我们把更多的时间留给我们更关注的业务逻辑,这里给一个之前的文章链接,读了过后可能你会更好的理解该篇文章的意义《Api写法第三篇》
废话不多说了,我们看一下我们今天的主角儿,@ControllerAdvice注解,这个是spring3.2提供的新注解,从名字上可以看出大体意思是控制器增强。今天我们就是使用它,来全局捕获系统中的异常,并对接口返回的的HTTP状态码做控制,下面来看代码的实现

代码实现

package com.zhuma.demo.handler;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.zhuma.demo.comm.handler.BaseAggregationLayerGlobalExceptionHandler;
import com.zhuma.demo.comm.result.Result;
import com.zhuma.demo.exception.BusinessException;

/**
 * @desc 统一异常处理器
 * 
 * @author zhumaer
 * @since 8/31/2017 3:00 PM
 */
@RestController
@ControllerAdvice
public class GlobalExceptionHandler extends BaseAggregationLayerGlobalExceptionHandler {

	/* 处理400类异常 */
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	@ExceptionHandler(ConstraintViolationException.class)
	public Result handleConstraintViolationException(ConstraintViolationException e, HttpServletRequest request) {
		return super.handleConstraintViolationException(e, request);
	}

	@ResponseStatus(HttpStatus.BAD_REQUEST)
	@ExceptionHandler(HttpMessageNotReadableException.class)
	public Result handleConstraintViolationException(HttpMessageNotReadableException e, HttpServletRequest request) {
		return super.handleConstraintViolationException(e, request);
	}

	@ResponseStatus(HttpStatus.BAD_REQUEST)
	@ExceptionHandler(BindException.class)
	public Result handleBindException(BindException e, HttpServletRequest request) {
		return super.handleBindException(e, request);
	}

	@ResponseStatus(HttpStatus.BAD_REQUEST)
	@ExceptionHandler(MethodArgumentNotValidException.class)
	public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) {
		return super.handleMethodArgumentNotValidException(e, request);
	}

	/* 处理自定义异常 */
	@ExceptionHandler(BusinessException.class)
	public ResponseEntity<Result> handleBusinessException(BusinessException e, HttpServletRequest request) {
		return super.handleBusinessException(e, request);
	}

	/* 处理运行时异常 */
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(RuntimeException.class)
	public Result handleRuntimeException(RuntimeException e, HttpServletRequest request) {
		return super.handleRuntimeException(e, request);
	}

}

解释说明

  • 这里我们使用@ControllerAdvice注解,如果是在写resetful风格的接口时,别忘记它是跟@RestController联用的哦
  • 这个类只是我们的定义,真正的实现逻辑是在其继承BaseAggregationLayerGlobalExceptionHandler 类中的,这样做的目的是因为在微服务系统中很多逻辑是可以共用的,所以向上抽离下代码还是很有必要的。
  • 目前处理了简单的参数错误异常(HTTP状态码:400)、运行时异常(HTTP状态码:500)和自定义异常(HTTP状态码:根据情况定)这几大类,其中自定义业务异常会细化我们在系统中常用到的情况,例如:数据找不到时,会抛出DataNotFoundException返回HTTP状态码404,没有权限时,会抛出PermissionForbiddenException异常返回401状态码,数据已存在也就是数据冲突会抛出DataConflictException返回409等等的异常,所有的自定义异常都会继承我们BusinessException业务异常,后面我会写一个关于自定义异常的文章,到时我们在更加详细的说明其使用。

统一异常处理实现类:

package com.zhuma.demo.comm.handler;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;

import com.zhuma.demo.comm.result.ParameterInvalidItem;
import com.zhuma.demo.comm.result.Result;
import com.zhuma.demo.enums.ExceptionEnum;
import com.zhuma.demo.enums.ResultCode;
import com.zhuma.demo.exception.BusinessException;
import com.zhuma.demo.util.ConvertUtil;

/**
 * @desc 聚合层全局异常处理类
 * 
 * @author zhumaer
 * @since 10/10/2017 9:54 AM
 */
public class BaseAggregationLayerGlobalExceptionHandler {

	private static final Logger LOGGER = LoggerFactory.getLogger(BaseAggregationLayerGlobalExceptionHandler.class);

	/**
	 * 违反约束异常
	 */
	protected Result handleConstraintViolationException(ConstraintViolationException e, HttpServletRequest request) {
		LOGGER.info("handleConstraintViolationException start, uri:{}, caused by: ", request.getRequestURI(), e);
		List<ParameterInvalidItem> parameterInvalidItemList = ConvertUtil.convertCVSetToParameterInvalidItemList(e.getConstraintViolations());
		return Result.failure(ResultCode.PARAM_IS_INVALID, parameterInvalidItemList);
	}

	/**
	 * 处理验证参数封装错误时异常
	 */
	protected Result handleConstraintViolationException(HttpMessageNotReadableException e, HttpServletRequest request) {
		LOGGER.info("handleConstraintViolationException start, uri:{}, caused by: ", request.getRequestURI(), e);
		return Result.failure(ResultCode.PARAM_IS_INVALID, e.getMessage());
	}

	/**
	 * 处理参数绑定时异常(反400错误码)
	 */
	protected Result handleBindException(BindException e, HttpServletRequest request) {
		LOGGER.info("handleBindException start, uri:{}, caused by: ", request.getRequestURI(), e);
		List<ParameterInvalidItem> parameterInvalidItemList = ConvertUtil.convertBindingResultToMapParameterInvalidItemList(e.getBindingResult());
		return Result.failure(ResultCode.PARAM_IS_INVALID, parameterInvalidItemList);
	}

	/**
	 * 处理使用@Validated注解时,参数验证错误异常(反400错误码)
	 */
	protected Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) {
		LOGGER.info("handleMethodArgumentNotValidException start, uri:{}, caused by: ", request.getRequestURI(), e);
		List<ParameterInvalidItem> parameterInvalidItemList = ConvertUtil.convertBindingResultToMapParameterInvalidItemList(e.getBindingResult());
		return Result.failure(ResultCode.PARAM_IS_INVALID, parameterInvalidItemList);
	}

	/**
	 * 处理通用自定义业务异常
	 */
	protected ResponseEntity<Result> handleBusinessException(BusinessException e, HttpServletRequest request) {
		LOGGER.info("handleBusinessException start, uri:{}, exception:{}, caused by: {}", request.getRequestURI(), e.getClass(), e.getMessage());

		ExceptionEnum ee = ExceptionEnum.getByEClass(e.getClass());
		if (ee != null) {
			return ResponseEntity
					.status(ee.getHttpStatus())
					.body(Result.failure(ee.getResultCode(), e.getData()));
		}

		return ResponseEntity
				.status(HttpStatus.OK)
				.body(e.getResultCode() == null ? Result.failure(e.getMessage()) : Result.failure(e.getResultCode(), e.getData()));
	}

	/**
	 * 处理运行时系统异常(反500错误码)
	 */
	protected Result handleRuntimeException(RuntimeException e, HttpServletRequest request) {
		LOGGER.error("handleRuntimeException start, uri:{}, caused by: ", request.getRequestURI(), e);
		//TODO 可通过邮件、微信公众号等方式发送信息至开发人员、记录存档等操作
		return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
	}

}

解释说明

  • 上面的代码如果你粘贴至本地是会有错误的,因为我们并没有将代码完全贴出,这里只是给你提供一个异常处理的思路,如果你想要代码具体实现的话,可以关注文章下面公众号,或私聊我进行获取。
  • 在处理RuntimeException 时,我们可通过邮件、微信公众号、短信等等方式发送信息至开发人员并记录存档等操作,这对线上的排查错误很是有帮助的(当然如果你们有自己的监控系统或使用开源的cat系统可以在此处做埋点处理)。
  • 也许你会注意到我们的异常实现类叫聚合层全局异常处理类,这是因为我们使用的是微服务架构,服务会分为 网关层、聚合层、原子层,对于异常可能每个层次的处理逻辑会有一些不同,这里我们给了最常用的处理业务聚合层的异常处理逻辑,如果你正在研究微服务架构或苦恼不知道该怎么入手,也可以私聊我O(∩_∩)O~。

全局异常处理注意事项

  • 要根据不同的异常返回合适的HTTP状态码
  • 异常结果返回时,不要将详细的异常堆栈信息由接口返回给前端(这可能会暴漏一些敏感信息,造成安全隐患,再者说前端开发根本不会关心这些)
  • 约定HTTP状态码为500时,都是后端程序上的错误,一定要修复掉
  • 对于系统的错误,一定要详细的打印好日志,要将异常的详细堆栈上下文信息都打印出来(有的同学只打印e.getMessage()当问题出现时,就变得很尴尬,难以一眼定位问题),方便线上排查问题

最后

若上述文字有描述不清楚,或者你有哪里不太懂的可以评论留言或者关注我的公众号,公众号里也会不定期的发送一些关于企业实战相关的文章哈!

源码github地址:https://github.com/zhumaer/zhuma
QQ群号:629446754(欢迎加群)

欢迎关注我们的公众号或加群,等你哦!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值