java后端接口对返回数据进行封装工具类

BaseResult.java

package com.basic.hellorabbit.util;

import java.io.Serializable;
import java.util.List;

/**
* @Description:    对返回数据进行封装
* @Author:         Joe
* @CreateDate:     2020/3/12 0:06
*/
public class BaseResult implements Serializable {

    public static final String RESULT_OK = "ok";
    public static final String RESULT_NOT_OK = "not_ok";
    public static final String SUCCESS = "成功操作";

    /**
     * 信息说明
     */
    private String result;
    /**
     * 数据
     */
    private Object data;
    /**
     * 成功说明
     */
    private String success;
    /**
     * 分页信息
     */
    private Cursor cursor;
    /**
     * 错误集合
     */
    private List<Error> errors;

    public static String getResultOk() {
        return RESULT_OK;
    }

    public static String getResultNotOk() {
        return RESULT_NOT_OK;
    }

    public static String getSUCCESS() {
        return SUCCESS;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getSuccess() {
        return success;
    }

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

    public Cursor getCursor() {
        return cursor;
    }

    public void setCursor(Cursor cursor) {
        this.cursor = cursor;
    }

    public List<Error> getErrors() {
        return errors;
    }

    public void setErrors(List<Error> errors) {
        this.errors = errors;
    }

    /**
     * 成功,无数据,有说明
     * @return
     */
    public static BaseResult ok(){
        return createResult(RESULT_OK,null,SUCCESS,null,null);
    }
    /**
     * 成功,无数据,自定义说明
     * @return
     */
    public static BaseResult ok(String success){
        return createResult(RESULT_OK,null,success,null,null);
    }
    /**
     * 成功,有数据,有说明
     * @return
     */
    public static BaseResult ok(Object data){
        return createResult(RESULT_OK,data,SUCCESS,null,null);
    }
    /**
     * 成功,有数据,有说明,带分页信息
     * @return
     */
    public static BaseResult ok(Object data, Cursor cursor){
        return createResult(RESULT_OK,data,SUCCESS,cursor,null);
    }

    /**
     * 失败,无数据,无说明,带错误信息
     * @return
     */
    public static BaseResult notOk(List<BaseResult.Error> errors){
        return createResult(RESULT_NOT_OK,null,"",null,errors);
    }
    /**
     * 失败,无数据,无说明,无错误信息
     * @return
     */
    public static BaseResult notOk(){
        return createResult(RESULT_NOT_OK,null,"",null,null);
    }

    private static BaseResult createResult(String result,Object data,String success,Cursor cursor,List<Error> errors){
        BaseResult baseResult = new BaseResult();
        baseResult.setResult(result);
        baseResult.setData(data);
        baseResult.setSuccess(success);
        baseResult.setCursor(cursor);
        baseResult.setErrors(errors);
        return baseResult;
    }

    /**
     * 分页信息类
     */
    public static class Cursor{
        /**
         * 总条数
         */
        private int total;
        /**
         *当前页
         */
        private int offset;
        /**
         * 每页条数
         */
        private int limit;
    }

    /**
     * 错误信息类
     */
    public static class Error{
        /**
         * 字段
         */
        private String field;
        /**
         * 错误描述
         */
        private String message;

        public String getField() {
            return field;
        }

        public void setField(String field) {
            this.field = field;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public Error() {
        }

        public Error(String field, String message) {
            this.field = field;
            this.message = message;
        }

        @Override
        public String toString() {
            return "Error{" +
                    "field='" + field + '\'' +
                    ", message='" + message + '\'' +
                    '}';
        }
    }
}

你可能感兴趣的:(常用代码块与笔记)