脚本宝典收集整理的这篇文章主要介绍了统一后端返回前端的json的数据格式,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
1. 定义enum类
enum类详解
package com.zhx.utils; public enum ResultCode { SUCCESS(1,"成功"), /*参数错误*/ PARam_IS_INVALID(1001, "参数无效"), PARAM_IS_BLANK(1002,"参数为空"), PARAM_TYPE_BIND_ERROR(1003, "参数类型错误"), PARAM_NOT_complete(1004, "参数缺失"), /*用户错误: 2001-2999*/ USER_NOT_LOGGED_IN(2001, "用户未登录,访问的路径需要验证,请登录") , USER_LOgin_ERROR(2002,"账号不存在或密码错误"), USER_ACCOUNT_FORBIDDEN(2003, "账号已被禁用"), USER_NOT_EXIST(2004, "用户不存在"), USER_HAS__EXISTED(2005, "用户已存在"); PRivate Integer code; private String msg; ResultCode(Integer code, String msg) { this.code = code; this.msg = msg; } public Integer getCode() { return code; } public String getMsg() { return msg; } }
2. 定义要返回的结果类
package com.zhx.utils; public class ResponseResult { private Object data; private ResultCode resultCode; public ResponseResult(ResultCode resultCode, Object data) { this.resultCode = resultCode; this.data = data; } public ResponseResult() { } public static ResponseResult success(){ ResponseResult responseResult = new ResponseResult(); responseResult.setResultCode(ResultCode.SUCCESS); return responseResult; } public static ResponseResult success(Object data){ ResponseResult responseResult = new ResponseResult(); responseResult.setResultCode(ResultCode.SUCCESS); responseResult.setData(data); return responseResult; } public static ResponseResult failure(ResultCode resultCode){ ResponseResult responseResult = new ResponseResult(); responseResult.setResultCode(resultCode); return responseResult; } public static ResponseResult failure(ResultCode resultCode,Object data){ ResponseResult responseResult = new ResponseResult(); responseResult.setResultCode(resultCode); responseResult.setData(data); return responseResult; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public ResultCode getResultCode() { return resultCode; } public void setResultCode(ResultCode resultCode) { this.resultCode = resultCode; } }
3. 优化
以上是脚本宝典为你收集整理的统一后端返回前端的json的数据格式全部内容,希望文章能够帮你解决统一后端返回前端的json的数据格式所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。