本文共 7892 字,大约阅读时间需要 26 分钟。
时间真快,转眼,博主已经是毕业工作两个月的社会人士了。虽然心里面从没有忘记“煤矿井下精确人员定位系统”开发总结的分享,但是却往往力不从心。投入工作后,一个月也没有几天的空闲时间,所以一直拖至今日。
1、简介
一个好的Web项目,是不会出现服务端对前端请求响应返回结果结构大量不一的情况的。所以就要求我们在Project开发之前,前后端开发人员要有一套约定。这样不仅便于管理,加快开发效率,还能够在一定程度上保证有效的维护以及数据管理。
当下,大多项目开发过程中采用技术为Spring系列框架,在一定程度上促进了JSON的使用。配合上Spring提供的@ResponseBody注解,的确是一绝。好了,接下来就开始响应结果包装类的构建吧!
2、响应结果分析
通常情况下,后端返回给前端的结果由状态码(code),响应信息(message),数据(data)构成。而在接下来,我们把状态码,响应信息划分为元信息meta一组,即为meta(code, message);数据data自行一组来进行划分。如此一来,响应结果结构为Response(meta, data)。
2.1 如何安置元信息meta
针对每一个请求,不论结果是响应成功还是失败,meta信息都是必须有的。也就是说是必不可少的,就像牛和腿的关系一样,所以我们需要把meta作为内部类来进行处理。所以当前结构为:
public class Response { private Meta meta; private Object data; class Meta { private int code; private boolean success; private String message; public Meta(boolean success) { this.code = ErrorMsg.SYSTEM_ERROR.getCode(); this.success = success; this.message = ErrorMsg.SYSTEM_ERROR.getMessage(); } public Meta(boolean success, String message) { this(success); this.message = message; } public Meta(boolean success, ErrorMsg errorMsg) { this(success, errorMsg.getMessage()); this.code = errorMsg.getCode(); } public int getCode() { return code; } public boolean isSuccess() { return success; } public String getMessage() { return message; } @Override public String toString() { return "Meta [code=" + code + ", success=" + success + ", message=" + message + "]"; } }}2.2 响应数据data都有哪些类型?
在Java中,响应数据类型无非为Object, Map,List,Set等。然后Map, List, Set也都是Object的子类,所以我们统一采用Object进行数据的保存。但是针对数据的存放,我们都更习惯于Key-Value形式。所以,这样就需要我们自行包装。构建如下结构:
public class Response { private Meta meta; private Object data; /** * 向data中放入组合数据结果集([Object | List | Map | Set | ...]) * * @param key * @param value * @return */ @SuppressWarnings("unchecked") public Response put(String key, Object value) { if (data == null || !(data instanceof Map)) { data = new HashMap2.3 如何构建Response以及提取结果?
鉴于响应有成功失败之分,我们需要针对Response提供success和failure两种构建方式。
2.3.1 success
然而,针对处理成功的请求,可能不需要返回数据,此时只是需要元信息;其次,也可能需要一些简单数据,不需要K-V形式。所以我们需要提供两种success构建Response方式,如下:
/** * 存在复杂组合数据对象或者没有数据对象的成功响应 * * @return */ public Response success() { this.meta = new Meta(true, OK); return this; } /** * 存在单个包装数据对象的成功响应 * * @param data * @return */ public Response success(Object data) { this.meta = new Meta(true, OK); this.data = data; return this; }2.3.2 failure
针对处理失败的请求,我们是没有响应数据的,只包括元信息。但是针对元信息,我们却可以自行进行定制或采用提前设置好的元信息。其次,也可以不指定元信息,采用默认元信息。所以我们提供3种failure构建Response的方式。
提前设置好的元信息枚举类:
public enum ErrorMsg { TEST(1, "test"), SUCCESS(200, "请求成功"), LOGIN_FAIL(101, "登录失败"), LOGOUT_SUCCESS(104, "登出成功"), USER_NOT_FOUND(102, "用户不存在"), USER_DISABLED(103, "无效用户"), USER_PASSWORD_ERROR(107, "密码错误"), NO_LOGIN(105, "未登录"), VERIFY_CODE_ERROR(106, "验证码错误"), SYSTEM_ERROR(100, "系统错误"); private int code; private String message; ErrorMsg(int code, String message) { this.code = code; this.message = message; } public int getCode() { return code; } public String getMessage() { return message; }}针对failure的3种构建Response方式:
/** * 没有详情提示的失败响应 * * @return */ public Response failure() { this.meta = new Meta(false, Error); return this; } /** * 存在详情提示的失败响应 * * @param message * @return */ public Response failure(String message) { this.meta = new Meta(false, message); return this; } /** * 存在详情提示的失败响应 * * @param errorMsg * @return */ public Response failure(ErrorMsg errorMsg) { this.meta = new Meta(false, errorMsg); return this; }2.4 Response的最终结构
import java.util.HashMap;import java.util.Map;public class Response { private static final String OK = "ok"; private static final String Error = "error"; private Meta meta; private Object data; /** * 存在复杂组合数据对象或者没有数据对象的成功响应 * * @return */ public Response success() { this.meta = new Meta(true, OK); return this; } /** * 存在单个包装数据对象的成功响应 * * @param data * @return */ public Response success(Object data) { this.meta = new Meta(true, OK); this.data = data; return this; } /** * 没有详情提示的失败响应 * * @return */ public Response failure() { this.meta = new Meta(false, Error); return this; } /** * 存在详情提示的失败响应 * * @param message * @return */ public Response failure(String message) { this.meta = new Meta(false, message); return this; } /** * 存在详情提示的失败响应 * * @param errorMsg * @return */ public Response failure(ErrorMsg errorMsg) { this.meta = new Meta(false, errorMsg); return this; } /** * 向data中放入组合数据结果集([Object | List | Map | Set | ...]) * * @param key * @param value * @return */ @SuppressWarnings("unchecked") public Response put(String key, Object value) { if (data == null || !(data instanceof Map)) { data = new HashMap2.5 Response响应包装类在后端的使用(); } ((Map ) data).put(key, value); return this; } /** * 针对没有返回数据结果集的及早求值操作 * * @return */ public Map toSimpleResult() { Map result = new HashMap (); result.put("meta", meta); return result; } /** * 针对有返回数据结果集的及早求值操作 * * @return */ public Map toCombineResult() { Map result = new HashMap (); result.put("meta", meta); result.put("data", data); return result; } class Meta { private int code; private boolean success; private String message; public Meta(boolean success) { this.code = ErrorMsg.SYSTEM_ERROR.getCode(); this.success = success; this.message = ErrorMsg.SYSTEM_ERROR.getMessage(); } public Meta(boolean success, String message) { this(success); this.message = message; } public Meta(boolean success, ErrorMsg errorMsg) { this(success, errorMsg.getMessage()); this.code = errorMsg.getCode(); } public int getCode() { return code; } public boolean isSuccess() { return success; } public String getMessage() { return message; } @Override public String toString() { return "Meta [code=" + code + ", success=" + success + ", message=" + message + "]"; } } @Override public String toString() { return "Response [meta=" + meta + ", data=" + data + "]"; }}
/** * @description 添加一个员工信息 * @param staff * @return */ @RequestMapping(value = "/base/staff", method = RequestMethod.POST) @SystemLogController(opType="添加",opContent="添加一个新的员工") public Map当然,针对没有响应数据的success Response,代码编写形式为: response.success().toSimpleResult();addStaff (@RequestBody Staff staff) { Response response = new Response(); int result = 0; try { result = staffService.insert(staff); } catch (SQLException e) { e.printStackTrace(); } if (result <= 0) { return response.failure("添加员工失败,请重试").toSimpleResult(); } return response.success().put("result", result).toCombineResult(); }
针对非K-V形式的success Response,代码编写形式为:response.success(data).toCombineResult();
针对使用定制元信息的failure Response,代码为:response.failure(ErrorMsg.SYSTEM_ERROR).toSimpleResult();
针对简单型failure Response,代码为:response.failure().toSimpleResult();
最后,需要说明的是,该Response是链式操作加及早求值的。也就是说你只要没有调用toSimpleResult()或者toCombineResult()方法,就不会产生结果,那么可以一直调用put(key, value)方法设置数据。但是,切记,put方法仅为success Response服务。
好了,后端响应包装类完成了。当然,你可以根据自己的需求进行修改调整,二次包装。博主设计,仅供参考!
转载地址:https://buildupchao.blog.csdn.net/article/details/77449502 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!