Java 返回值规范(封装返回对象)
所需枚举类
public enum CodeEnum {SUCCESS(0),ERROR(1);private Integer code;private CodeEnum(Integer code) {this.code = code;}public Integer getCode() {return this.code;}
}
简单对象返回:
返回值示例:
{"datas": ,"resp_code": 0,"resp_msg": ""
}
类:
import java.io.Serializable;public class Result implements Serializable {private T datas;private Integer resp_code;private String resp_msg;public static Result succeed(String msg) {return of((Object)null, CodeEnum.SUCCESS.getCode(), msg);}public static Result succeed(T model, String msg) {return of(model, CodeEnum.SUCCESS.getCode(), msg);}public static Result succeed(T model) {return of(model, CodeEnum.SUCCESS.getCode(), "SUCCESS");}public static Result of(T datas, Integer code, String msg) {return new Result(datas, code, msg);}public static Result failed(String msg) {return of((Object)null, CodeEnum.ERROR.getCode(), msg);}public static Result failed(T model, String msg) {return of(model, CodeEnum.ERROR.getCode(), msg);}public static Result failed(BusinessExceptionEnum businessExceptionEnum) {return of((Object)null, businessExceptionEnum.getCode(), businessExceptionEnum.getMessage());}public static Result failed(BusinessExceptionEnum businessExceptionEnum, String message) {return of((Object)null, businessExceptionEnum.getCode(), message);}public T getDatas() {return this.datas;}public Integer getResp_code() {return this.resp_code;}public String getResp_msg() {return this.resp_msg;}public void setDatas(T datas) {this.datas = datas;}public void setResp_code(Integer resp_code) {this.resp_code = resp_code;}public void setResp_msg(String resp_msg) {this.resp_msg = resp_msg;}public boolean equals(Object o) {if (o == this) {return true;} else if (!(o instanceof Result)) {return false;} else {Result> other = (Result)o;if (!other.canEqual(this)) {return false;} else {label47: {Object this$resp_code = this.getResp_code();Object other$resp_code = other.getResp_code();if (this$resp_code == null) {if (other$resp_code == null) {break label47;}} else if (this$resp_code.equals(other$resp_code)) {break label47;}return false;}Object this$datas = this.getDatas();Object other$datas = other.getDatas();if (this$datas == null) {if (other$datas != null) {return false;}} else if (!this$datas.equals(other$datas)) {return false;}Object this$resp_msg = this.getResp_msg();Object other$resp_msg = other.getResp_msg();if (this$resp_msg == null) {if (other$resp_msg != null) {return false;}} else if (!this$resp_msg.equals(other$resp_msg)) {return false;}return true;}}}protected boolean canEqual(Object other) {return other instanceof Result;}public int hashCode() {int PRIME = true;int result = 1;Object $resp_code = this.getResp_code();int result = result * 59 + ($resp_code == null ? 43 : $resp_code.hashCode());Object $datas = this.getDatas();result = result * 59 + ($datas == null ? 43 : $datas.hashCode());Object $resp_msg = this.getResp_msg();result = result * 59 + ($resp_msg == null ? 43 : $resp_msg.hashCode());return result;}public String toString() {return "Result(datas=" + this.getDatas() + ", resp_code=" + this.getResp_code() + ", resp_msg=" + this.getResp_msg() + ")";}public Result() {}public Result(T datas, Integer resp_code, String resp_msg) {this.datas = datas;this.resp_code = resp_code;this.resp_msg = resp_msg;}
}
复杂对象返回(如分页对象)
返回值示例:
{"datas": [{}],"resp_code": 0,"resp_msg": "","total": 0
}
类:
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.io.Serializable;
import java.util.List;public class PageResult implements Serializable {private static final long serialVersionUID = -275582248840137389L;private Integer total;private Integer resp_code;private String resp_msg;private List datas;public static PageResult result(long total, List list) {return new PageResult(Math.toIntExact(total), 200, "success", list);}public static PageResult result(Integer total, List list) {return new PageResult(total, 200, "success", list);}public static PageResult result(Integer total, List list, String msg) {return new PageResult(total, 200, msg, list);}public static PageResult result(Page page) {return new PageResult(Convert.toInt(page.getTotal()), 200, "success", page.getRecords());}public static PageResult.PageResultBuilder builder() {return new PageResult.PageResultBuilder();}public Integer getTotal() {return this.total;}public Integer getResp_code() {return this.resp_code;}public String getResp_msg() {return this.resp_msg;}public List getDatas() {return this.datas;}public void setTotal(Integer total) {this.total = total;}public void setResp_code(Integer resp_code) {this.resp_code = resp_code;}public void setResp_msg(String resp_msg) {this.resp_msg = resp_msg;}public void setDatas(List datas) {this.datas = datas;}public boolean equals(Object o) {if (o == this) {return true;} else if (!(o instanceof PageResult)) {return false;} else {PageResult> other = (PageResult)o;if (!other.canEqual(this)) {return false;} else {label59: {Object this$total = this.getTotal();Object other$total = other.getTotal();if (this$total == null) {if (other$total == null) {break label59;}} else if (this$total.equals(other$total)) {break label59;}return false;}Object this$resp_code = this.getResp_code();Object other$resp_code = other.getResp_code();if (this$resp_code == null) {if (other$resp_code != null) {return false;}} else if (!this$resp_code.equals(other$resp_code)) {return false;}Object this$resp_msg = this.getResp_msg();Object other$resp_msg = other.getResp_msg();if (this$resp_msg == null) {if (other$resp_msg != null) {return false;}} else if (!this$resp_msg.equals(other$resp_msg)) {return false;}Object this$datas = this.getDatas();Object other$datas = other.getDatas();if (this$datas == null) {if (other$datas != null) {return false;}} else if (!this$datas.equals(other$datas)) {return false;}return true;}}}protected boolean canEqual(Object other) {return other instanceof PageResult;}public int hashCode() {int PRIME = true;int result = 1;Object $total = this.getTotal();int result = result * 59 + ($total == null ? 43 : $total.hashCode());Object $resp_code = this.getResp_code();result = result * 59 + ($resp_code == null ? 43 : $resp_code.hashCode());Object $resp_msg = this.getResp_msg();result = result * 59 + ($resp_msg == null ? 43 : $resp_msg.hashCode());Object $datas = this.getDatas();result = result * 59 + ($datas == null ? 43 : $datas.hashCode());return result;}public String toString() {return "PageResult(total=" + this.getTotal() + ", resp_code=" + this.getResp_code() + ", resp_msg=" + this.getResp_msg() + ", datas=" + this.getDatas() + ")";}public PageResult() {}public PageResult(Integer total, Integer resp_code, String resp_msg, List datas) {this.total = total;this.resp_code = resp_code;this.resp_msg = resp_msg;this.datas = datas;}public static class PageResultBuilder {private Integer total;private Integer resp_code;private String resp_msg;private List datas;PageResultBuilder() {}public PageResult.PageResultBuilder total(Integer total) {this.total = total;return this;}public PageResult.PageResultBuilder resp_code(Integer resp_code) {this.resp_code = resp_code;return this;}public PageResult.PageResultBuilder resp_msg(String resp_msg) {this.resp_msg = resp_msg;return this;}public PageResult.PageResultBuilder datas(List datas) {this.datas = datas;return this;}public PageResult build() {return new PageResult(this.total, this.resp_code, this.resp_msg, this.datas);}public String toString() {return "PageResult.PageResultBuilder(total=" + this.total + ", resp_code=" + this.resp_code + ", resp_msg=" + this.resp_msg + ", datas=" + this.datas + ")";}}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
