欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot的异常处理

程序员文章站 2022-07-14 11:03:59
...

SpringBoot的异常处理

  1. 在SpringBoot的WEB应用中,如果控制器抛出异常,并没有处理的话,都会统一转发到一个error.html的错误结果页面,此页面由SpringBoot(spring-boot-starter-web)提供。
  2. SpringBoot的WEB应用中,会自动的提供一个映射,URL是/error,处理这个请求的类型是BasicErrorController,其中的处理逻辑是将异常信息封装到作用域中,并传递给视图’error’。
  3. 如果需要提供一个统一的错误页面处理异常,可以在系统中提供一个error.html来实现

BasicErrorController收集的错误信息包含:
error - 错误描述,如: Internal Server Error (服务内部错误)
exception - 异常类型, 如: java.lang. ArithmeticException
message - 异常描述, 如: / by zero
timestamp - 时间戳
status - 响应状态码, 如: 200, 500, 404等

##ExceptionHandler

  1. Spring支持异常的细致化处理,可以在控制器中定义若干方法,专门用于处理异常。处理异常的方法使用注解**@ExceptionHandler**来描述,注解的value属性是Class[]类型,代表该方法可以处理的异常种类。
  2. 这种异常处理方式力度较细,处理方式多样化。其缺陷是: 如果定义了太多的异常处理方法,会提升维护成本;且异常处理方法只对当前控制器有效。代码有bad smell。
  3. 这种开发方式适合针对性处理。因为定义在控制器中的异常处理方法处理优先级最高

##ControllerAdvice和ExceptionHandler

  1. SpringBoot可以独立定义一个类型作为ControllerAdvice(Controller的一种增强),类型需要使用@ControllerAdvice注解来描述,类型中可以定义若干异常处理方法,且方法使用@ExceptionHandler来描述。
  2. 当应用中发生异常时,异常的处理顺序是: 当前控制器中定义的@ExceptionHandler方法 -> @ControllerAdvice类中定义的@ExceptionHandler方法 -> BasicErrorController中定义的服务方法(error.html默认异常页面)
  3. 可以解决异常处理通用性。这种处理异常的方式,需要定义一个Advice类型和若干异常处理方法。
@ControllerAdvice
public class ExceptionControllerAdvice {

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

	@ExceptionHandler(BusinessException.class)
	@ResponseBody
	public Result<?> handleBusinessException(HttpServletRequest request,
			Exception e) {
		return handleException(request, e, false);
	}

	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	@ResponseBody
	public ResponseEntity<Integer> handleMethodNotSupportedException(HttpServletRequest request,
			Exception e) {
		return ResponseEntity.badRequest().body(null);
	}

	@ExceptionHandler(Exception.class)
	@ResponseBody
	public Result<?> handleOtherException(HttpServletRequest request,
			Exception e) {
		return handleException(request, e, true);
	}

	private Result<?> handleException(HttpServletRequest request,
			Exception e,
			boolean logger) {
		String errMsg = ExceptionUtils.getErrorMessage(e);
		String strError = StringUtils.isEmpty(errMsg) ? e.getMessage() : errMsg;
		ResultCode respondCode = ResultCode.get(strError);
		if (logger) {
			LOGGER.error(strError, e);
			e.printStackTrace();
		}
		if (respondCode == null) {
			return Result.error(strError);
		} else {
			return Result.create(respondCode);
		}
	}
}