package com.hx.resultTool; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.hx.exception.TipsException; import java.io.Serializable; /** * 统一返回格式 * @author chenjiahe * @Data: 2020-06-20 */ public class Result implements Serializable { private static final long serialVersionUID = -3948389268046368059L; private String code; private String msg; private Object data; public Result() {} public Result(String code, String msg) { this.code = code; this.msg = msg; } public static Result success() { Result Result = new Result(); Result.setCode(ResponseCode.SUCCESS); Result.setMsg("SUCCESS"); return Result; } public static Result success(Object data) { Result Result = new Result(); Result.setCode(ResponseCode.SUCCESS); Result.setData(data); Result.setMsg("SUCCESS"); return Result; } public static Result failure(String code, String msg) { Result Result = new Result(); Result.setCode(code); Result.setMsg(msg); return Result; } public static Result failure(String code, String msg, Object data) { Result Result = new Result(); Result.setCode(code); Result.setMsg(msg); Result.setData(data); return Result; } /**校验返回码*/ public Boolean checkCode(){ if(ResponseCode.SUCCESS.equals(code)){ return true; } return false; } /**校验返回码,进行错误提示*/ public void checkTips(){ if(!ResponseCode.SUCCESS.equals(code)){ throw new TipsException("请求失败:"+this.code+","+this.msg); } } /**返回数据转JSONObject*/ public JSONObject getJsonObject(Object data){ return JSONObject.parseObject(JSON.toJSONString(data)); } /**返回数据转JSONArray*/ public JSONArray getJsonArray(Object data){ return JSONArray.parseArray(JSON.toJSONString(data)); } public T getObject(Object data, Class clazz) { return JSONObject.parseObject(JSON.toJSONString(data), clazz); } /*******************************************************************************/ public String getCode() { return this.code; } public void setCode(final String code) { this.code = code; } public String getMsg() { return this.msg; } public void setMsg(final String msg) { this.msg = msg; } public Object getData() { return this.data; } public void setData(final Object data) { this.data = data; } }