文章目录
一、全局异常处理
1、自定义业务异常类
2、全局异常处理类
二、配置钉钉机器人
1、创建钉钉机器人
三、整合钉钉机器人
1、引入jar包
2、发送信息的工具类
3、测试
4、常见报错
四、所用到的工具类
一、全局异常处理
1、自定义业务异常类
某些时候,由于业务逻辑需要抛出自定义异常,这个时候就需要自定义业务异常类。
定义CommonException,使他继承于RuntimeException.
说明:因为某些业务需要进行业务回滚。但spring的事务只针对RuntimeException的进行回滚操作。所以需要回滚就要继承RuntimeException。
public class CommonException extends RuntimeException {
private Integer errCode;
private String errMsg;
public Integer getErrCode() {
return errCode;
}
public void setErrCode(Integer errCode) {
this.errCode = errCode;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public CommonException(Integer errCode, String errMsg) {
this.errCode = errCode;
this.errMsg = errMsg;
}
public CommonException(String errMsg, Throwable e) {
super(errMsg);
this.errMsg = errMsg;
}
}
2、全局异常处理类
import com.adleading.daJinMember.common.constant.CodeMsg;
import com.adleading.daJinMember.common.constant.Result;
import com.adleading.daJinMember.common.utils.GlobalUtils;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.stereotype.Component;