Java 之返回对象
1、ResultCodeEnum.java
package com.vue.test.demo.entity; /** * @author yangwj * @date 2020/3/22 16:59 */ public enum ResultCodeEnum { SUCCESS(true,200,"成功"), UNKNOWN_ERROR(false,401,"未知参数"), PARAM_ERROR(false,402,"参数错误"), UPLOAD_FILE_ERROR(false,10000,"上传文件失败!") ; private Boolean success; private Integer code; private String message; public Boolean getSuccess() { return success; } public void setSuccess(Boolean success) { this.success = success; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } ResultCodeEnum(Boolean success, Integer code, String message) { this.success = success; this.code = code; this.message = message; } }
2、Result.java
package com.vue.test.demo.entity; /** * @author yangwj * @date 2020/3/22 17:18 */ public class Result<T> { private Integer code; private String msg; private T data; public Result() { super(); } public Result(Integer code, String msg, T data) { this.code = code; this.msg = msg; this.data = data; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; } @Override public String toString() { return "Result{" + "code=" + code + ", msg='" + msg + '\'' + ", data=" + data + '}'; } }
3、返回工具类ResponseResult.java
package com.vue.test.demo.entity; /** * @author yangwj * @date 2020/3/22 17:19 */ public class ResponseResult { /**成功且带数据**/ public static Result success(Object object){ Result result = new Result(); result.setCode(ResultCodeEnum.SUCCESS.getCode()); result.setMsg(ResultCodeEnum.SUCCESS.getMessage()); result.setData(object); return result; } /**成功但不带数据**/ public static Result success(){ return success(null); } /**失败**/ public static Result error(Integer code,String msg){ Result result = new Result(); result.setCode(code); result.setMsg(msg); return result; } }
本文来自博客园,作者:小白啊小白,Fighting,转载请注明原文链接:https://www.cnblogs.com/ywjfx/p/12547274.html