數(shù)據(jù)返回格式
- 正常返回
-
applicaiton/json
格式進(jìn)行返回數(shù)據(jù) -
data
具體的業(yè)務(wù)數(shù)據(jù) -
http status code
為200 -
traceId
為唯一的請求id
-
- 業(yè)務(wù)邏輯異常,為了統(tǒng)一返回的數(shù)據(jù)結(jié)構(gòu)足丢,統(tǒng)一使用了zalando/problem項(xiàng)目作為返回的數(shù)據(jù)結(jié)構(gòu)粱腻。主要包括:
- 定義統(tǒng)一的業(yè)務(wù)異常類,然后統(tǒng)一捕獲該異常類斩跌,然后轉(zhuǎn)換數(shù)據(jù)結(jié)構(gòu)绍些。
-
applicaiton/json
格式進(jìn)行返回數(shù)據(jù) -
detail
為對應(yīng)的錯誤信息簡單描述 -
code
為對應(yīng)的業(yè)務(wù)錯誤碼 -
type
為異常請求的uri -
http status code
為200 -
traceId
為唯一的請求id
- 請求異常,統(tǒng)一識別異常類型耀鸦,然后轉(zhuǎn)換數(shù)據(jù)結(jié)構(gòu)
-
applicaiton/json
格式進(jìn)行返回數(shù)據(jù) -
detail
為對應(yīng)的錯誤信息簡單描述 -
code
為對應(yīng)的業(yè)務(wù)錯誤碼 -
type
為異常請求的uri -
http status code
為400柬批、401、403揭糕、404萝快、405、406著角、415 -
traceId
為唯一的請求id
-
- 非業(yè)務(wù)異常類,統(tǒng)一處理
-
applicaiton/json
格式進(jìn)行返回數(shù)據(jù) -
detail
為對應(yīng)的錯誤信息簡單描述 -
code
為對應(yīng)的錯誤碼 -
type
為異常請求的uri -
http status code
為500旋恼、501 -
traceId
為唯一的請求id
-
統(tǒng)一異常捕獲
- 通過繼承zalando/problem-spring-web項(xiàng)目的
ProblemHandling
類 - 通過使用
@RestControllerAdvice
來統(tǒng)一捕獲異常 - 額外定義業(yè)務(wù)異常類的統(tǒng)一處理方法
- 通過使用
ResponseBodyAdvice
接口和@RestControllerAdvice
來統(tǒng)一添加traceId和請求的uri