開發系統中,先後端分離,後端通常返回RESTfull API,前端調用API構建UI,彼此分離、互相徹底獨立;前端
後臺API中,咱們通常返回結果碼、提示信息、數據三部份內容,如圖:git
咱們今天就來構建一個完整的先後端分離後端的APIgithub
package com.anson.common.result; /** * @Author anson * @Description 一、結果碼接口 * @Date 2019/12/10 10:50 */ public interface IErrorCode { long getCode(); String getMessage(); }
package com.anson.common.result; /** * @Author anson * @Description 結果消息枚舉 * @Date 2019/12/10 22:05 */ public enum ResultCode implements IErrorCode { // 數據操做消息定義 SUCCESS(200, "成功!"), BODY_NOT_MATCH(400,"請求的數據格式不符!"), UNAUTHORIZED(401,"暫未登陸或token已通過期!"), FORBIDDEN(403, "沒有相關權限"), NOT_FOUND(404, "未找到該資源!"), FAILED(500, "服務器內部錯誤!"), SERVER_BUSY(503,"服務器正忙,請稍後再試!"); private long code; private String message; private ResultCode(long code, String message) { this.code = code; this.message = message; } public long getCode() { return code; } public String getMessage() { return message; } }
package com.anson.common.result; /** * @description: API返回結果實體 * @author: anson * @Date: 2019/12/10 10:54 */ public class ResultBody<T> { private long code; private String message; private T data; protected ResultBody() { } protected ResultBody(long code, String message, T data) { this.code = code; this.message = message; this.data = data; } /** * 成功返回結果 * * @param data 獲取的數據 */ public static <T> ResultBody<T> success(T data) { return new ResultBody<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data); } /** * 成功返回結果 * * @param data 獲取的數據 * @param message 提示信息 */ public static <T> ResultBody<T> success(T data, String message) { return new ResultBody<T>(ResultCode.SUCCESS.getCode(), message, data); } /** * 失敗返回結果 * * @param errorCode 錯誤碼 */ public static <T> ResultBody<T> failed(IErrorCode errorCode) { return new ResultBody<T>(errorCode.getCode(), errorCode.getMessage(), null); } /** * 失敗返回結果 * * @param message 提示信息 */ public static <T> ResultBody<T> failed(String message) { return new ResultBody<T>(ResultCode.FAILED.getCode(), message, null); } /** * 失敗返回結果 */ public static <T> ResultBody<T> failed() { return failed(ResultCode.FAILED); } /** * 參數驗證失敗返回結果 */ public static <T> ResultBody<T> validateFailed() { return failed(ResultCode.NOT_FOUND); } /** * 參數驗證失敗返回結果 * * @param message 提示信息 */ public static <T> ResultBody<T> validateFailed(String message) { return new ResultBody<T>(ResultCode.NOT_FOUND.getCode(), message, null); } /** * 未登陸返回結果 */ public static <T> ResultBody<T> unauthorized(T data) { return new ResultBody<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data); } /** * 未受權返回結果 */ public static <T> ResultBody<T> forbidden(T data) { return new ResultBody<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data); } public long getCode() { return code; } public void setCode(long code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public T getData() { return data; } public void setData(T data) { this.data = data; } }
略,具體看代碼,跟前幾節的一致後端
//--------------------------------API test------------------- //一、根據ID獲取單個用戶 @ApiOperation(value = "獲取用戶", notes = "根據id查詢用戶信息") @ApiImplicitParam(name = "id", value = "用戶id", required=true, dataType="int") //API參數 @RequestMapping(value="/user/{id}",method= RequestMethod.GET) public ResultBody getUserById(int id) { User user = userservice.selectByPrimaryKey(id); return ResultBody.success(user,"獲取單個信息成功"); } //二、獲取全部用戶 @ApiOperation(value = "獲取全部用戶", notes = "獲取全部用戶") @RequestMapping(value="/userall",method= RequestMethod.GET) public ResultBody getUserAll() { List<User> users = userservice.getAll(); return ResultBody.success(users,"獲取全部用戶信息成功"); }
好,完畢服務器
{ "code": 200, "data": [ { "id": 1, "password": "123", "realname": "張三", "username": "anson" }, { "id": 2, "password": "123", "realname": "李四", "username": "Alex" }, { "id": 3, "password": "123", "realname": "王五", "username": "kks" }, { "id": 4, "password": "000", "realname": "趙柳", "username": "eee" } ], "message": "獲取全部用戶信息成功" }
GIT源碼地址:https://github.com/anson-yang/cloverDemo.gitapp