api-resultAPI 接口返回结果规范化解决方案
API 接口开发返回结果规范化解决方案
一、概述
Api Result,是 RESTful风格的API接口响应参数规范化的一套解决方案 。它是在实际应用环境下产生了,并不断更新和完善。 结合Spring Boot进行开发,让接口响应结果变得更加规范。
二、示例
{
"code":"111111",
"message":"Query Success",
"success":true,
"trace_id":"edf36a73796356d5bc9b6d81f4eb83d6",
"body":{
"categoryData":[
{
"id":"1265200246055809026",
"categoryName":"公告"
},
{
"id":"1257301514647777282",
"categoryName":"Java"
},
{
"id":"1257694082590060546",
"categoryName":"开发"
},
{
"id":"1257255039406452737",
"categoryName":"前端"
},
{
"id":"1257687302015299586",
"categoryName":"计划"
},
{
"id":"1258792800529727490",
"categoryName":"网站更新日志"
},
{
"id":"1257256132635013121",
"categoryName":"日志"
}
]
}
}
三、实体类属性介绍
CommonResponse
| 序号 | 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|---|
| 1 | code | String | 返回码 | |
| 2 | message | String | 返回码描述信息 | |
| 3 | success | Boolean | false | 响应结果状态,true:表示成功;false:表示失败 |
| 4 | traceId | String | 追溯码,json格式返回时,为 trace_id |
|
| 5 | header | Map<String, String> | 响应头 | |
| 6 | body | 泛型 <T> |
响应体 |
CommonPage
| 序号 | 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|---|
| 1 | currentPage | Long | 当前页 | |
| 2 | pageSize | Integer | 每页显示条数 | |
| 3 | totalElements | Long | 总条数 | |
| 4 | totalPages | Long | 总页数 | |
| 5 | content | 泛型 <T> |
数据 |
四、返回方法介绍
返回成功
-
CommonResponse.ok()
-
CommonResponse.ok(data)
返回失败
-
CommonResponse.error()
-
CommonResponse.error(msg)
-
CommonResponse.error(code, msg)
-
CommonResponse.error(iError)
五、IError介绍
IError是返回码和描述信息的接口。
你可以在返回时,使用。
为了方便,提供了常见的错误,将其命名为 Default。
你可以直接使用 IError.Default。
值得注意的是:成功的返回码是6个1,字符型,111111 。
六、中央仓库
评论
