JAVA错误类结果类和分页结果类代码详解

网友投稿 271 2022-12-15


JAVA错误类结果类和分页结果类代码详解

这篇文章主要介绍了java错误类结果类和分页结果类代码详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

代码如下

public interface ErrorCode {

String getCode();

String getMsg();

/**

* 公共错误码

* 码段:10000~10099

*

*

*/

public enum CommonError implements ErrorCode {

SUCCESS("0", "success"),

UNKNOWN_ERROR("10000", "未知错误"),

SYSTEM_ERROR("10001", "系统内部处理异常[%s]"),

REQUEST_ERROR("10002", "请求地址或参数错误"),

DB_PROCESS_FAILED("10003", "数据库处理失败"),

DATA_NOT_FOUND("10004", "数据不存在"),

PARAM_REQUIRED("10005", "参数为空[%s]");

private String code;

private String msg;

private CommonError(String code, String msg) {

this.code = code;

this.msg = msg;

}

@Override

public String getCode() {

return this.code;

}

@Override

public String getMsg() {

return this.msg;

}

public ErrorCode setParams(Object... params) {

this.msg = String.format(this.msg, params);

return this;

}

}

/**

* 接口错误码

* 码段:10100~10199

*

*/

public enum ApiError implements ErrorCode {

REQUEST_EMPTY("10100", "请求数据为空"),

API_NOT_EXISTS("10102"http://, "接口不存在");

private String code;

private String msg;

private ApiError(String code, String msg) {

this.code = code;

this.msg = msg;

}

@Override

public String getCode() {

return this.code;

}

@Override

public String getMsg() {

return this.msg;

}

}

}

ErrorCode error = CommonError.SYSTEM_ERROR.setParams(e.getCause().getMessage());

Result:

public class Result {

public static final String SUCCESS = "0";

public static final String SCUCESS_MSG = "success";

private String result = SUCCESS;

private String msg = SCUCESS_MSG;

private T data;

public Result() {}

public Result(ErrorCode error, Object... msgParams) {

this.setError(error, msgParams);

}

public Result(ErrorCode error) {

this.setError(error);

}

public void setError(ErrorCode error, Object... msgParams) {

this.result = error.getCode();

this.msg = error.getMsg();

if (msgParams != null && msgParams.length > 0) {

this.msg = String.format(msg, msgParams);

}

}

public void setError(ErrorCode error) {

this.setError(error, new Object[] {});

}

public String getResult() {

return result;

}

public void setResult(String result) {

this.result = result;

}

public String getMsg() {

return msg;

}

public void setMsg(String msg) {

this.msghttp:// = msg;

}

public T getData() {

return data;

}

public void setData(T data) {

this.data = data;

}

public boolean isSuccess() {

return SUCCESS.equals(result);

}

@Override

public String toSthttp://ring() {

return "Result [result=" + result + ", msg=" + msg + ", data=" + data + "]";

}

}

PageResult:

public class PageResult {

public static final String SUCCESS = "0";

public static final String SCUCESS_MSG = "success";

private String result = SUCCESS;

private String msg = SCUCESS_MSG;

private Integer total = 0;

private List rows = new ArrayList<>();

public PageResult() {}

public PageResult(ErrorCode error, Object... msgParams) {

this.setError(error, msgParams);

}

public PageResult(ErrorCode error) {

this.setError(error);

}

public String getResult() {

return result;

}

public void setResult(String result) {

this.result = result;

}

public String getMsg() {

return msg;

}

public void setMsg(String msg) {

this.msg = msg;

}

public Integer getTotal() {

return total;

}

public void setTotal(Integer total) {

this.total = total;

}

public List getRows() {

return rows;

}

public void setRows(List rows) {

this.rows = rows;

}

public void setError(ErrorCode error, Object... msgParams) {

this.result = error.getCode();

this.msg = error.getMsg();

if (msgParams != null && msgParams.length > 0) {

this.msg = String.format(msg, msgParams);

}

}

public void setError(ErrorCode error) {

this.setError(error, new Object[] {});

}

public boolean isSuccess() {

return SUCCESS.equals(result);

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring Bean装载方式代码实例解析
下一篇:Java并发编程之常用的多线程实现方式分析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~