1 摘要
REST 風格已經成爲 WEB 開發的主流趨勢,本文將分享作者自己在項目中使用的java後臺 REST API 接口封裝類。
核心代碼
2.1 Swagger Maven 依賴
Swagger 作爲可視化接口測試工具,根據代碼自動生成接口文檔,方便用戶測試,所以可以集成到項目中
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
2.2 接口返回結果封裝
com.ljq.demo.springboot.rest.common.api.ApiResult.java
package com.ljq.demo.springboot.rest.common.api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.Map;
/**
* @Description: 接口返回結果封裝
* @Author: junqiang.lu
* @Date: 2019/3/26
*/
@Data
@ApiModel(value = "接口返回結果")
public class ApiResult<T> implements Serializable {
private static final long serialVersionUID = -2953545018812382877L;
/**
* 返回碼,200 正常
*/
@ApiModelProperty(value = "返回碼,200 正常", name = "code")
private int code = 200;
/**
* 返回信息
*/
@ApiModelProperty(value = "返回信息", name = "msg")
private String msg = "成功";
/**
* 返回數據
*/
@ApiModelProperty(value = "返回數據對象", name = "data")
private T data;
/**
* 附加數據
*/
@ApiModelProperty(value = "附加數據", name = "extraData")
private Map<String, Object> extraData;
/**
* 系統當前時間
*/
@ApiModelProperty(value = "服務器系統時間,時間戳(精確到毫秒)", name = "timestamp")
private Long timestamp = System.currentTimeMillis();
/**
* 獲取成功狀態結果
*
* @return
*/
public static ApiResult success() {
return success(null, null);
}
/**
* 獲取成功狀態結果
*
* @param data 返回數據
* @return
*/
public static ApiResult success(Object data) {
return success(data, null);
}
/**
* 獲取成功狀態結果
*
* @param data 返回數據
* @param extraData 附加數據
* @return
*/
public static ApiResult success(Object data, Map<String, Object> extraData) {
ApiResult apiResult = new ApiResult();
apiResult.setCode(ResponseCode.SUCCESS.getCode());
apiResult.setMsg(ResponseCode.SUCCESS.getMsg());
apiResult.setData(data);
apiResult.setExtraData(extraData);
return apiResult;
}
/**
* 獲取失敗狀態結果
*
* @return
*/
public static ApiResult failure() {
return failure(ResponseCode.FAIL.getCode(), ResponseCode.FAIL.getMsg(), null);
}
/**
* 獲取失敗狀態結果
*
* @param msg (自定義)失敗消息
* @return
*/
public static ApiResult failure(String msg) {
return failure(ResponseCode.FAIL.getCode(), msg, null);
}
/**
* 獲取失敗狀態結果
*
* @param responseCode 返回狀態碼
* @return
*/
public static ApiResult failure(ResponseCode responseCode) {
return failure(responseCode.getCode(), responseCode.getMsg(), null);
}
/**
* 獲取失敗狀態結果
*
* @param responseCode 返回狀態碼
* @param data 返回數據
* @return
*/
public static ApiResult failure(ResponseCode responseCode, Object data) {
return failure(responseCode.getCode(), responseCode.getMsg(), data);
}
/**
* 獲取失敗返回結果
*
* @param code 錯誤碼
* @param msg 錯誤信息
* @param data 返回結果
* @return
*/
public static ApiResult failure(int code, String msg, Object data) {
ApiResult result = new ApiResult();
result.setCode(code);
result.setMsg(msg);
result.setData(data);
return result;
}
}
2.3 使用說明
code
: 接口返回狀態碼
msg
: 接口返回信息說明
data
: 接口返回數據
extraData
: 附加數據。當接口需要增加返回數據時,將其通過 extraData
傳遞,這樣可以保證原來前端的接口數據結構不變,減少出故障概率。
timestamp
: 當前服務器系統時間戳,可用於獲取服務器時間,也可用於作爲作爲數據有效性校驗
2.4 Github 源碼
Github 項目源碼: https://github.com/Flying9001/springBootDemo
個人公衆號:404Code,記錄半個互聯網人的技術與思考,感興趣的可以關注.