文章目錄
- 一、全域例外處理
- 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;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
@Component
@Slf4j
public class CommonExceptionHandler {
@Autowired
private GlobalUtils globalUtils;
@ExceptionHandler
@ResponseBody
@ResponseStatus(HttpStatus.OK)
public Result handle(Exception exception) throws ApiException {
if (exception instanceof CommonException) {
log.error(exception.getMessage(), exception);
CommonException e = (CommonException) exception;
log.error("捕獲到例外,例外代碼為{},例外資訊為{}", e.getErrCode(), e.getErrMsg());
//發送例外資訊到釘釘機器人
globalUtils.globalMsg(exception);
return Result.error(new CodeMsg(Integer.valueOf(e.getErrCode()), e.getErrMsg()));
} else if (exception instanceof HttpMessageNotReadableException) {
log.error("前臺傳入JSON格式錯誤");
//發送例外資訊到釘釘機器人
globalUtils.globalMsg(exception);
return Result.error(CodeMsg.JSON_ERR);
} else {
//發送例外資訊到釘釘機器人
globalUtils.globalMsg(exception);
log.error("捕獲到例外錯誤");
return Result.error(CodeMsg.FILES);
}
}
}
注意:這里用到的一些工具類,會在文章的結尾處貼出來,
二、配置釘釘機器人
這里給大家簡單說了一下如何創建釘釘機器人,如果想深入了解的,點擊傳送門到官方檔案處,
1、創建釘釘機器人
-
點擊左下角更多選擇機器人管理

-
然后選擇自定義

- 添加機器人

1、輸入機器人名字并選擇要發送訊息的群
2、完成必要的安全設定(至少選擇一種,博主這里選擇了打 √ 的兩個)
3、當選擇了自定義關鍵詞的時候,在發送報警資訊的時候一定要帶上關鍵詞,(后面代碼演示會指出)
- 完成設定后,復制出機器人的Webhook地址,可用于向這個群發送訊息,格式如下:
https://oapi.dingtalk.com/robot/send?access_token=XXXXXX
三、整合釘釘機器人
1、引入jar包
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>1.0.1</version>
</dependency>
2、發送資訊的工具類
當前自定義機器人支持文本 (text)、鏈接 (link)、markdown(markdown)、ActionCard、FeedCard訊息型別,這里就只演示文本 (text)訊息型別,

3、測驗
- Controller層
@Resource
private TestService testService;
@GetMapping("/test")
public Result Test() {
testService.test();
return new Result(CodeMsg.SUCCESS);
}
2.Service層
@Service
public class TestServiceImpl implements TestService {
@Override
public void test() {
int i = 1 / 0;
}
}
- 測驗結果如下

4、常見報錯
// 訊息內容中不包含任何關鍵詞
{
"errcode":310000,
"errmsg":"keywords not in content"
}
// timestamp 無效
{
"errcode":310000,
"errmsg":"invalid timestamp"
}
// 簽名不匹配
{
"errcode":310000,
"errmsg":"sign not match"
}
// IP地址不在白名單
{
"errcode":310000,
"errmsg":"ip X.X.X.X not in whitelist"
}
四、所用到的工具類
- CodeMsg類
public class CodeMsg {
private int code;
private String msg;
public CodeMsg(int code, String msg) {
this.code = code;
this.msg = msg;
}
public CodeMsg() {
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public static CodeMsg SUCCESS = new CodeMsg(200, "success");
public static CodeMsg FILES = new CodeMsg(300, "未知錯誤");
public static CodeMsg JSON_ERR = new CodeMsg(300, "JSON格式錯誤");
}
- Result類
@ApiModel(value = "回應結果")
public class Result<T> {
@ApiModelProperty(position = 0, value = "狀態碼", required = true, example = "200200")
private int code;
@ApiModelProperty(position = 1, value = "回應資訊", required = true, example = "success")
private String msg;
@ApiModelProperty(position = 2, value = "回應資料", required = true)
private T data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
/**
* * 成功時候的呼叫
* *
*/
public static <T> Result<T> success(T data) {
return new Result<T>(data);
}
/**
* * 失敗時候的呼叫
* *
*/
public static <T> Result<T> error(CodeMsg codeMsg) {
return new Result<T>(codeMsg);
}
/**
* * 成功的建構式
* * @param data
*
*/
private Result(T data) {
this.code = 000000;//默認000000是成功
this.msg = "SUCCESS";
this.data = data;
}
public Result(int code, String msg) {
this.code = code;
this.msg = msg;
}
public Result(int code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public Result(CodeMsg codeMsg, T data) {
this.code = codeMsg.getCode();
this.msg = codeMsg.getMsg();
this.data = data;
}
/**
* * 失敗的建構式
* * @param code
* * @param msg
*
*/
public Result(CodeMsg codeMsg) {
if (codeMsg != null) {
this.code = codeMsg.getCode();
this.msg = codeMsg.getMsg();
}
}
}
- GlobalUtils類
@Component
public class GlobalUtils {
@Autowired
private DingTalkUtils dingTalkUtils;
public void globalMsg(Exception exception) throws ApiException {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
exception.printStackTrace(pw);
String stackTraceString = sw.getBuffer().toString();
dingTalkUtils.sendMsg(stackTraceString);
}
}
這篇檔案還有很多需要優化的地方,如果有朋友有好的想法或者建議,可以聯系我一起交流一下,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/205291.html
標籤:其他
上一篇:因為多執行緒,我懟了一個同事
