# :-: API響應結果
## 為什么要統一API響應結果
* 對于移動端和PC端開發而言,調用接口返回有統一的響應體,可以針對性的設計界面,代碼結構更加清晰,層次也更加分明。
* 統一根據返回的狀態碼判斷接口調用的具體情況,從而進行下一步操作
* 統一根據返回的的消息字段顯示接口調用的具體狀態
* 統一根據返回的數據承載進行數據的交互與展示
## API 響應類代碼
~~~
/**
* 統一API響應結果封裝
*
* @author Chill
*/
@Getter
@Setter
@ToString
@ApiModel(description = "返回信息")
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class R<T> implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "狀態碼", required = true)
private int code;
@ApiModelProperty(value = "是否成功", required = true)
private boolean success;
@ApiModelProperty(value = "承載數據")
private T data;
@ApiModelProperty(value = "返回消息", required = true)
private String msg;
private R(IResultCode resultCode) {
this(resultCode, null, resultCode.getMessage());
}
private R(IResultCode resultCode, String msg) {
this(resultCode, null, msg);
}
private R(IResultCode resultCode, T data) {
this(resultCode, data, resultCode.getMessage());
}
private R(IResultCode resultCode, T data, String msg) {
this(resultCode.getCode(), data, msg);
}
private R(int code, T data, String msg) {
this.code = code;
this.data = data;
this.msg = msg;
this.success = ResultCode.SUCCESS.code == code;
}
/**
* 判斷返回是否為成功
*
* @param result Result
* @return 是否成功
*/
public static boolean isSuccess(@Nullable R<?> result) {
return Optional.ofNullable(result)
.map(x -> ObjectUtil.nullSafeEquals(ResultCode.SUCCESS.code, x.code))
.orElse(Boolean.FALSE);
}
/**
* 判斷返回是否為成功
*
* @param result Result
* @return 是否成功
*/
public static boolean isNotSuccess(@Nullable R<?> result) {
return !R.isSuccess(result);
}
/**
* 返回R
*
* @param data 數據
* @param <T> T 泛型標記
* @return R
*/
public static <T> R<T> data(T data) {
return data(data, BladeConstant.DEFAULT_SUCCESS_MESSAGE);
}
/**
* 返回R
*
* @param data 數據
* @param msg 消息
* @param <T> T 泛型標記
* @return R
*/
public static <T> R<T> data(T data, String msg) {
return data(HttpServletResponse.SC_OK, data, msg);
}
/**
* 返回R
*
* @param code 狀態碼
* @param data 數據
* @param msg 消息
* @param <T> T 泛型標記
* @return R
*/
public static <T> R<T> data(int code, T data, String msg) {
return new R<>(code, data, data == null ? BladeConstant.DEFAULT_NULL_MESSAGE : msg);
}
/**
* 返回R
*
* @param msg 消息
* @param <T> T 泛型標記
* @return R
*/
public static <T> R<T> success(String msg) {
return new R<>(ResultCode.SUCCESS, msg);
}
/**
* 返回R
*
* @param resultCode 業務代碼
* @param <T> T 泛型標記
* @return R
*/
public static <T> R<T> success(IResultCode resultCode) {
return new R<>(resultCode);
}
/**
* 返回R
*
* @param resultCode 業務代碼
* @param msg 消息
* @param <T> T 泛型標記
* @return R
*/
public static <T> R<T> success(IResultCode resultCode, String msg) {
return new R<>(resultCode, msg);
}
/**
* 返回R
*
* @param msg 消息
* @param <T> T 泛型標記
* @return R
*/
public static <T> R<T> failure(String msg) {
return new R<>(ResultCode.FAILURE, msg);
}
/**
* 返回R
*
* @param code 狀態碼
* @param msg 消息
* @param <T> T 泛型標記
* @return R
*/
public static <T> R<T> failure(int code, String msg) {
return new R<>(code, null, msg);
}
/**
* 返回R
*
* @param resultCode 業務代碼
* @param <T> T 泛型標記
* @return R
*/
public static <T> R<T> failure(IResultCode resultCode) {
return new R<>(resultCode);
}
/**
* 返回R
*
* @param resultCode 業務代碼
* @param msg 消息
* @param <T> T 泛型標記
* @return R
*/
public static <T> R<T> failure(IResultCode resultCode, String msg) {
return new R<>(resultCode, msg);
}
/**
* 返回R
*
* @param flag 成功狀態
* @return R
*/
public static R status(boolean flag) {
return flag ? success(BladeConstant.DEFAULT_SUCCESS_MESSAGE) : failure(BladeConstant.DEFAULT_FAILURE_MESSAGE);
}
}
~~~
## 如何使用
1. 擼起袖子來優化下上一章我們新建的 API
~~~
@GetMapping("info")
@PreAuth("hasRole('administrator')")
public R<String> info(String name) {
return R.data("Hello, My Name Is: " + name);
}
@GetMapping("count")
@PreAuth("permitAll()")
public R<Integer> count(Integer cnt) {
return R.data(cnt * 10);
}
~~~
2. 調用 API 查看具體返回
演示結果待放。。。。。
3. 可以看到,返回接口變成了Json,并且字段清晰,分別代表 狀態碼、是否成功、數據承載、消息
## 注
* 每個 API 都需要規范,具有統一的響應結果,這樣才更利于后續的開發
* 移動端、PC端對接的時候,也請根據`success`來判斷接口是否成功而不是根據`code`來判斷。
- 序
- 快速開始
- 環境要求
- 環境準備
- 工程導入
- 工程運行
- 技術基礎
- Java8
- Lambda
- Lambda 受檢異常處理
- Stream 簡介
- Stream API 一覽
- Stream API(上)
- Stream API(下)
- Optional 干掉空指針
- 函數式接口
- 新的日期 API
- Lombok
- SpringMVC
- Swagger
- Mybaties
- Mybaties-plus
- 開發初探
- 新建微服務工程
- 第一個API
- API鑒權
- API響應結果
- Redis 緩存
- 第一個CRUD
- 建表
- 建Entity
- 建Service和Mapper
- 新增API
- 修改API
- 刪除API
- 查詢API
- 單條查詢
- 多條查詢
- 分頁
- 微服務遠程調用
- 聲明式服務調用Feign
- 熔斷機制 Hystrix
- 開發進階