当前位置:首页 > IT技术 > 编程语言 > 正文

spring 统一异常处理类
2022-04-25 22:58:28

@ControllerAdvice
public class ExceptionAdvice {

//    处理业务异常
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity handaleException(BusinessException be){
//        打印
        be.printStackTrace();
//        拿到业务异常对象
        ErrorResult errorResult = be.getErrorResult();
//        返回信息
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult.getErrMessage());
    }

//    处理其他异常
    @ExceptionHandler(Exception.class)
    public ResponseEntity handleOthers(Exception e){
        e.printStackTrace();
//        返回系统异常
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.error());

    }
}

本文摘自 :https://www.cnblogs.com/