分页对象、状态码、返回结果对象、自定义异常

分页对象

/**
 * 分页工具类
 */
public class PageInfo<T> implements Serializable {

    private static final long serialVersionUID = 1800935089461387955L;

    /**
     * 总记录数
     */
    private Integer total;
    /**
     * 每页记录数
     */
    private Integer pageSize;
    /**
     * 当前页数
     */
    private Integer pageNo;
    /**
     * 列表数据
     */
    private List<T> records;

    /**
     * 分页
     *
     * @param records    当前页列表数据
     * @param totalCount 总记录数
     * @param pageSize   每页记录数
     * @param currPage   当前页数
     */
    public PageInfo(List<T> records, int totalCount, int pageSize, int currPage) {
        this.records = records;
        this.total = totalCount;
        this.pageSize = pageSize;
        this.pageNo = currPage;
    }

    /**
     * 分页(主要用于内存分页, 每次都是查询全部数据再分页)
     *
     * @param records  总的列表数据
     * @param pageSize 每页记录数
     * @param currPage 当前页数
     */
    public PageInfo(List<T> records, int pageSize, int currPage) {
        if (records == null) {
            records = new ArrayList();
        }
        this.total = records.size();
        this.pageSize = pageSize;
        this.pageNo = currPage;
        //总记录数和每页显示的记录之间是否可以凑成整数(pages)
        boolean full = this.total % pageSize == 0;
        int pages = 0;
        if (!full) {
            pages = this.total / pageSize + 1;
        } else {
            pages = this.total / pageSize;
        }
        int fromIndex = 0;
        int toIndex = 0;
        fromIndex = pageNo * pageSize - pageSize;
        if (this.pageNo <= 0) {
            //如果查询的页数小于等于0,则展示第一页
            this.pageNo = 1;
            toIndex = pageSize;
        } else if (this.pageNo >= pages) {
            this.pageNo = pages;
            toIndex = this.total;
        } else {
            toIndex = this.pageNo * this.pageSize;
        }
        if (records.size() == 0) {
            this.records = records;
        } else {
            this.records = records.subList(fromIndex, toIndex);
        }
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public List<T> getRecords() {
        return records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }

    @Override
    public String toString() {
        return "PageInfo{" +
                "total=" + total +
                ", pageSize=" + pageSize +
                ", pageNo=" + pageNo +
                ", records=" + records +
                '}';
    }
}

状态码

/**
 * 操作提示类*/
@Data
public class CodeMsg {

    /******************** 通用错误码 ********************/
    public static CodeMsg SUCCESS = new CodeMsg(true, 10000, "success");
    public static CodeMsg SERVER_ERROR = new CodeMsg(false, 10001, "系统服务异常");
    public static CodeMsg BIND_ERROR = new CodeMsg(false, 10002, "参数校验异常");
    public static CodeMsg REQUEST_ILLEGAL = new CodeMsg(false, 10003, "请求非法");
    public static CodeMsg ACCESS_LIMIT_REACHED = new CodeMsg(false, 10004, "访问太频繁");
    public static CodeMsg PARAMETER_ERROR = new CodeMsg(false, 10005, "参数异常,%");
    public static CodeMsg REMOTE_REQUEST_ERROR = new CodeMsg(false, 10006, "远程调用失败");
    public static CodeMsg UNKNOWN_ERROR = new CodeMsg(false, 99998, "未知异常");
    public static CodeMsg SEARCH_DATA_EMPTY = new CodeMsg(false, 99999, "暂未查到相关信息");

    /**
     * 是否成功, true或false
     */
    private boolean success;

    /**
     * 操作编码
     */
    private int code;

    /**
     * 操作提示
     */
    private String msg;

    public CodeMsg(boolean success, int code, String msg) {
        this.success = success;
        this.code = code;
        this.msg = msg;
    }

    public CodeMsg fillArgs(Object... args) {
        int code = this.code;
        String msg = String.format(this.msg, args);
        return new CodeMsg(false, code, msg);
    }
}

返回结果对象

/**
 * 返回结果统一格式
 *
 * @param <T>
 */
public class ApiResult<T> implements Serializable {

    private static final long serialVersionUID = -9085242252623279681L;
    /**
     * 是否成功
     **/
    private Boolean success;
    /**
     * 状态码
     **/
    private Integer code;
    /**
     * 消息
     **/
    private String msg;
    /**
     * 返回数据
     **/
    private T data;

    public ApiResult(Integer code, String msg, T value) {
        this.code = code;
        this.msg = msg;
        this.data = value;
    }

    public ApiResult() {
    }

    private ApiResult(CodeMsg codeMsg) {
        if (codeMsg != null) {
            this.success = codeMsg.isSuccess();
            this.code = codeMsg.getCode();
            this.msg = codeMsg.getMsg();
        }
    }

    public static <T> ApiResult<T> success(T t) {
        ApiResult<T> result = new ApiResult(CodeMsg.SUCCESS);
        result.setData(t);
        return result;
    }

    public static <T> ApiResult<T> success() {
        ApiResult<T> result = new ApiResult(CodeMsg.SUCCESS);
        return result;
    }

    public static <T> ApiResult<T> error(CodeMsg codeMsg) {
        return new ApiResult<T>(codeMsg);
    }

    public static <T> ApiResult<T> error(Integer code, String msg) {
        if (null == code) {
            code = CodeMsg.UNKNOWN_ERROR.getCode();
        }
        if (null == msg || msg == "") {
            msg = CodeMsg.UNKNOWN_ERROR.getMsg();
        }
        ApiResult<T> result = new ApiResult();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public boolean isSuccess() {
        return this.code == CodeMsg.SUCCESS.getCode();
    }

    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{" +
                "success=" + success +
                ", code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}

自定义异常

public class BizException extends RuntimeException {

    private String msg;
    private int code = 500;

    public BizException(String msg) {
        super(msg);
        this.msg = msg;
    }

    public BizException(String msg, Throwable e) {
        super(msg, e);
        this.msg = msg;
    }

    public BizException(CodeMsg errorCode) {
        super(errorCode.getMsg());
        this.msg = errorCode.getMsg();
        this.code = errorCode.getCode();
    }

    public BizException(CodeMsg errorCode, String errorMsg) {
        super(errorMsg);
        this.msg = errorMsg;
        this.code = errorCode.getCode();
    }

    public BizException(String msg, int code) {
        super(msg);
        this.msg = msg;
        this.code = code;
    }

    public BizException(String msg, int code, Throwable e) {
        super(msg, e);
        this.msg = msg;
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }
}

断言工具:

/**
 * 断言工具类
 */
public class BizExceptionAssert {
    private BizExceptionAssert() {
    }

    public static void isTrue(boolean expression, CodeMsg errorCode) {
        if (!expression) {
            throw new BizException(errorCode);
        }
    }

    public static void isTrue(boolean expression, CodeMsg errorCode, String message) {
        if (!expression) {
            throw new BizException(errorCode, message);
        }
    }

    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new BizException(message);
        }
    }

    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new BizException(message);
        }
    }

    public static void notBlank(String text, String message) {
        if (StringUtils.isBlank(text)) {
            throw new BizException(message);
        }
    }


    public static void notEmpty(Object[] array, String message) {
        if (ArrayUtils.isEmpty(array)) {
            throw new BizException(message);
        }
    }

    public static void noNullElements(Object[] array, String message) {
        if (array != null) {
            for (Object element : array) {
                if (element == null) {
                    throw new BizException(message);
                }
            }
        }
    }

    public static void notEmpty(Collection<?> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BizException(message);
        }
    }

    public static void notEmpty(Map<?, ?> map, String message) {
        if (MapUtils.isEmpty(map)) {
            throw new BizException(message);
        }
    }
}

异常工具:

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 异常工具类
 **/
public final class ExceptionUtil {

    private ExceptionUtil() {
    }

    /**
     * 获取异常的堆栈信息
     *
     * @param t
     * @return
     */
    public static String getStackTrace(Throwable t) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);

        try {
            t.printStackTrace(pw);
            return sw.toString();
        } finally {
            pw.close();
        }
    }
}

 

posted @ 2022-01-06 11:13  残城碎梦  阅读(140)  评论(0编辑  收藏  举报