0、前言
开发系统中,前后端分离,后端一般返回RESTfull API,前端调用API构建UI,彼此分离、互相完全独立;
后台API中,我们一般返回结果码、提示信息、数据三部分内容,如图:
我们今天就来构建一个完整的前后端分离后端的API
一、RESTfull API构建
1、增加结果码接口 IErrorCode
package com.anson.common.result; /** * @Author anson * @Description 1、结果码接口 * @Date 2019/12/10 10:50 */ public interface IErrorCode { long getCode(); String getMessage(); }
2、添加结果消息枚举 ResultCode
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; } }
3、添加返回结果实体 ResultBody
package com.anson.common.result; /** * @description: API返回结果实体 * @author: anson * @Date: 2019/12/10 10:54 */ public class ResultBody{ 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 ResultBody success(T data) { return new ResultBody (ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data); } /** * 成功返回结果 * * @param data 获取的数据 * @param message 提示信息 */ public static ResultBody success(T data, String message) { return new ResultBody (ResultCode.SUCCESS.getCode(), message, data); } /** * 失败返回结果 * * @param errorCode 错误码 */ public static ResultBody failed(IErrorCode errorCode) { return new ResultBody (errorCode.getCode(), errorCode.getMessage(), null); } /** * 失败返回结果 * * @param message 提示信息 */ public static ResultBody failed(String message) { return new ResultBody (ResultCode.FAILED.getCode(), message, null); } /** * 失败返回结果 */ public static ResultBody failed() { return failed(ResultCode.FAILED); } /** * 参数验证失败返回结果 */ public static ResultBody validateFailed() { return failed(ResultCode.NOT_FOUND); } /** * 参数验证失败返回结果 * * @param message 提示信息 */ public static ResultBody validateFailed(String message) { return new ResultBody (ResultCode.NOT_FOUND.getCode(), message, null); } /** * 未登录返回结果 */ public static ResultBody unauthorized(T data) { return new ResultBody (ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data); } /** * 未授权返回结果 */ public static ResultBody forbidden(T data) { return new ResultBody (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; } }
4、添加实体类、dao、service
略,具体看代码,跟前几节的一致
5、编写controller
//--------------------------------API test------------------- //1、根据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,"获取单个信息成功"); } //2、获取所有用户 @ApiOperation(value = "获取所有用户", notes = "获取所有用户") @RequestMapping(value="/userall",method= RequestMethod.GET) public ResultBody getUserAll() { Listusers = userservice.getAll(); return ResultBody.success(users,"获取所有用户信息成功"); }
好,完毕
二、运行测试,在swagger中执行测试,结果如下
{ "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.git