统一异常

目标:我们想让异常结果也显示为统一的返回结果对象,并且统一处理系统的异常信息,那么需要进行统一异常处理。

1、创建统一异常处理器

guigu-common中创建exception包,创建统一异常处理器类UnifiedExceptionHandler

@Slf4j

@Component //Spring容易自动管理
@RestControllerAdvice //在controller层添加通知。如果使用@ControllerAdvice,则方法上需要添加@ResponseBody
public class UnifiedExceptionHandler {


    /**

     * 未定义异常

     */

    @ExceptionHandler(value = Exception.class) //当controller中抛出Exception,则捕获

    public R handleException(Exception e) {

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

        return R.error();

    }
}

2、service-core添加扫描

@SpringBootApplication
@ComponentScan({"com.xxxx.xxxx"})
public class ServiceCoreApplication {}

自定义异常

目标:使用一个或较少的异常类,可以捕获和显示所有的异常信息。

方案:因此,我们可以创建一个自定义异常类(必须是运行时异常),在程序中抛出这个自定义异常对象,并在统一异常处理器中捕获自定义异常对象

1、创建自定义异常类


@Data

@NoArgsConstructor

public class BusinessException extends RuntimeException {

    //状态码

    private Integer code;


    //错误消息

    private String message;

}

2、添加异常处理方法

UnifiedExceptionHandler类中添加

/**

* 自定义异常

*/

@ExceptionHandler(BusinessException.class)

public R handleBusinessException(BusinessException e){

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

    return R.error().message(e.getMessage()).code(e.getCode());

}

3、修改Controller

在Controller的方法中添加异常处理,业务中需要的位置抛出BusinessException自定义异常。

@ApiOperation("")

@PostMapping("/save")

public R save(

    @ApiParam(value = "积分等级对象", required = true)

    @RequestBody IntegralGrade integralGrade){

    //抛出一个自定义的异常!

    if(integralGrade.getBorrowAmount() == null){

        //BORROW_AMOUNT_NULL_ERROR(-201, "错误内容"),

        throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

    }


    boolean result = integrationService.save(integralGrade);

    if (result) {

        return R.ok().message("保存成功");

    } else {

        return R.error().message("保存失败");

    }

}

异常处理优化

目标:以优雅的 Assert(断言) 方式来校验业务的异常情况,消除 if else

自定义断言

3
@Slf4j
4public abstract class Assert {


    /**

     * 断言对象不为空

     * 如果对象obj为空,则抛出异常

     * @param obj 待判断对象

     */

    public static void notNull(Object obj, ResponseEnum responseEnum) {

        if (obj == null) {

            log.info("obj is null...............");

            throw new BusinessException(responseEnum);

        }

    }

}

修改controller

在controller中用断言替换if else

 Assert.notNull(integralGrade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

Controller上层异常

1、异常分类

对异常按阶段进行分类,大体可以分成:进入Controller前的异常 和 业务层异常,具体可以参考下图:

 

2、处理Controller上层异常

UnifiedExceptionHandler中添加

/**

     * Controller上一层相关异常

     */

@ExceptionHandler({

    NoHandlerFoundException.class,

    HttpRequestMethodNotSupportedException.class,

    HttpMediaTypeNotSupportedException.class,

    MissingPathVariableException.class,

    MissingServletRequestParameterException.class,

    TypeMismatchException.class,

    HttpMessageNotReadableException.class,

    HttpMessageNotWritableException.class,

    MethodArgumentNotValidException.class,

    HttpMediaTypeNotAcceptableException.class,

    ServletRequestBindingException.class,

    ConversionNotSupportedException.class,

    MissingServletRequestPartException.class,

    AsyncRequestTimeoutException.class

        })

public R handleServletException(Exception e) {

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

    //SERVLET_ERROR(-102, "servlet请求异常"),

    return R.error().message(ResponseEnum.SERVLET_ERROR.getMessage()).code(ResponseEnum.SERVLET_ERROR.getCode());

}

  • 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、付费专栏及课程。

余额充值