统一异常处理

String requestType = request.getHeader(“X-Requested-With”);
// 根据不同错误转向不同页面
if (ex instanceof BusinessException) {

	} else if(ex instanceof SystemException) {
		return "m/bind";
	} else if (ex instanceof DataAccessException) {
		ex = new SystemException( "数据库操作失败",ex);
	} else if (ex instanceof NullPointerException) {
		ex = new SystemException( "调用了未经初始化的对象或者是不存在的对象",ex);
	} else if (ex instanceof IOException) {
		ex = new SystemException( "IO异常",ex);
	} else if (ex instanceof ClassNotFoundException) {
		ex = new SystemException( "指定的类不存在",ex);
	} else if (ex instanceof ArithmeticException) {
		ex = new SystemException( "数学运算异常",ex);
	} else if (ex instanceof ArrayIndexOutOfBoundsException) {
		ex = new SystemException( "数组下标越界",ex);
	} else if (ex instanceof IllegalArgumentException) {
		ex = new SystemException( "方法的参数错误",ex);
	} else if (ex instanceof ClassCastException) {
		ex = new SystemException( "类型强制转换错误",ex);
	} else if (ex instanceof SecurityException) {
		ex = new SystemException( "违背安全原则异常",ex);
	} else if (ex instanceof SQLException) {
		ex = new SystemException( "操作数据库异常",ex);
	} else if (ex instanceof CannotCreateTransactionException) {
		ex = new SystemException("数据库连接失败",ex);
	} else if (ex instanceof TransactionSystemException) {
		ex = new SystemException( "数据库连接失败",ex);
	} else if (ex instanceof UnauthorizedException){
		ex = new SystemException("无权进行该操作",ex);
	} else if (ex instanceof Exception) {
		ex = new SystemException( "程序内部错误,操作失败",ex);
	}
	request.setAttribute("ex", ex); 
	if(StringUtils.isNotBlank(requestType)){
		//异步请求
        return "common/error/systemExceptionAjax";
	}
    return "common/error/systemException";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot提供了一种简单而强大的方式来处理应用程序中的异常,即统一异常处理。通过统一异常处理,我们可以捕获和处理应用程序中的所有异常,并返回自定义的错误响应。 在Spring Boot中,我们可以使用@ControllerAdvice注解来定义一个全局的异常处理类。这个类可以包含多个异常处理方法,每个方法对应一个具体的异常类型。当应用程序中抛出对应的异常时,Spring Boot会自动调用相应的异常处理方法进行处理。 下面是一个简单的示例代码,演示了如何使用@ControllerAdvice来实现统一异常处理: ```java @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity<ErrorResponse> handleException(Exception ex) { ErrorResponse errorResponse = new ErrorResponse(); errorResponse.setMessage("Internal Server Error"); errorResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); } @ExceptionHandler(UserNotFoundException.class) public ResponseEntity<ErrorResponse> handleUserNotFoundException(UserNotFoundException ex) { ErrorResponse errorResponse = new ErrorResponse(); errorResponse.setMessage("User Not Found"); errorResponse.setStatus(HttpStatus.NOT_FOUND.value()); return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND); } // 其他异常处理方法... } ``` 在上面的代码中,我们定义了两个异常处理方法:handleException和handleUserNotFoundException。handleException方法用于处理所有未被其他异常处理方法捕获的异常,而handleUserNotFoundException方法用于处理UserNotFoundException异常。 在每个异常处理方法中,我们可以根据具体的业务需求,创建一个自定义的错误响应对象,并将其封装在ResponseEntity中返回给客户端。这样,无论是哪种异常,都可以得到统一的错误响应。 需要注意的是,为了使统一异常处理生效,我们还需要在应用程序的配置类上添加@EnableWebMvc注解。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值