自定义枚举类和后端统一返回对象格式

自定义枚举类

package com.izkml.mlyun.uc.user.enums;

import java.util.*;

/**
 * 行政区划级别枚举
 * Created by qindong on 2019/3/5 0005
 */
public enum AdminDivisionLevelEnum {

    /**
     * 省(区、市)
     */
    PROVINCE("省(区、市)"),
    /**
     * 市(州、盟)
     */
    CITY("市(州、盟)"),
    /**
     * 县(区、旗)
     */
    COUNTY("县(区、旗)"),
    /**
     * 乡(镇)
     */
    VILLAGE("乡(镇)");

    /**
     * 枚举Set,包含本枚举的所以实例
     */
    private static EnumSet<AdminDivisionLevelEnum> enumSet = EnumSet.allOf(AdminDivisionLevelEnum.class);

    /**
     * 描述
     */
    private String description;

    /**
     * 获取文字描述
     *
     * @return 文字描述
     */
    public String getDescription() {
        return description;
    }

    /**
     * 设置文字描述
     *
     * @param description 文字描述
     */
    public void setDescription(String description) {
        this.description = description;
    }


    /**
     * 构造函数
     *
     * @param description 描述,不可为空
     */
    AdminDivisionLevelEnum(String description) {
        if (description == null) {
            throw new IllegalArgumentException("description不可为空");
        }
        this.description = description;
    }

    /**
     * 根据描述获取枚举
     *
     * @param description 描述
     * @return 枚举, 如果description为null或者找不到匹配的枚举, 返回null
     */
    public static AdminDivisionLevelEnum getByDescription(String description) {
        if (description == null) {
            return null;
        }
        for (Iterator<AdminDivisionLevelEnum> iter = enumSet.iterator(); iter.hasNext(); ) {
            AdminDivisionLevelEnum enumObject = iter.next();
            if (description.equals(enumObject.getDescription())) {
                return enumObject;
            }
        }
        return null;
    }

    public static List<Map> getEnumMap() {
        List<Map> result = new ArrayList<>();
        for (Iterator<AdminDivisionLevelEnum> iter = enumSet.iterator(); iter.hasNext(); ) {
            Map map = new HashMap();
            AdminDivisionLevelEnum enumObject = iter.next();
            map.put(enumObject.toString(), enumObject.getDescription());
            result.add(map);
        }
        return result;
    }

    public static Map<String, String> getEnumForMap() {
        Map<String, String> result = new HashMap<>();
        for (Iterator<AdminDivisionLevelEnum> iter = enumSet.iterator(); iter.hasNext(); ) {
            AdminDivisionLevelEnum enumObject = iter.next();
            result.put(enumObject.toString(), enumObject.getDescription());
        }
        return result;
    }
}

自定义JsonResult统一返回对象

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.izkml.mlyun.framework.common.result;

import com.izkml.mlyun.framework.common.exception.BaseException;

public class JsonResult<T> {
    private boolean success;
    private T data;
    private String msg;
    private String errCode;

    public JsonResult(boolean success, String msg) {
        this.success = true;
        this.success = success;
        this.msg = msg;
    }

    public JsonResult(boolean success, String msg, T data) {
        this(success, msg);
        this.data = data;
    }

    public static JsonResult ok() {
        return new JsonResult();
    }

    public static JsonResult ok(String msg) {
        return new JsonResult(true, msg);
    }

    public static <T> JsonResult ok(String msg, T data) {
        return new JsonResult(true, msg, data);
    }

    public static JsonResult error() {
        return new JsonResult(false, (String)null);
    }

    public static JsonResult error(String msg) {
        return new JsonResult(false, msg);
    }

    public static JsonResult error(String msg, String errCode) {
        return new JsonResult(false, (Object)null, msg, errCode);
    }

    public static JsonResult error(BaseException e) {
        return new JsonResult(false, e.getErrorCode(), e.getMessage(), (String)null);
    }

    public JsonResult(boolean success, T data, String msg, String errCode) {
        this.success = true;
        this.success = success;
        this.data = data;
        this.msg = msg;
        this.errCode = errCode;
    }

    public JsonResult() {
        this.success = true;
    }

    public boolean isSuccess() {
        return this.success;
    }

    public T getData() {
        return this.data;
    }

    public String getMsg() {
        return this.msg;
    }

    public String getErrCode() {
        return this.errCode;
    }

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

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

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

    public void setErrCode(String errCode) {
        this.errCode = errCode;
    }

    public boolean equals(Object o) {
        if(o == this) {
            return true;
        } else if(!(o instanceof JsonResult)) {
            return false;
        } else {
            JsonResult<?> other = (JsonResult)o;
            if(!other.canEqual(this)) {
                return false;
            } else if(this.isSuccess() != other.isSuccess()) {
                return false;
            } else {
                label49: {
                    Object this$data = this.getData();
                    Object other$data = other.getData();
                    if(this$data == null) {
                        if(other$data == null) {
                            break label49;
                        }
                    } else if(this$data.equals(other$data)) {
                        break label49;
                    }

                    return false;
                }

                Object this$msg = this.getMsg();
                Object other$msg = other.getMsg();
                if(this$msg == null) {
                    if(other$msg != null) {
                        return false;
                    }
                } else if(!this$msg.equals(other$msg)) {
                    return false;
                }

                Object this$errCode = this.getErrCode();
                Object other$errCode = other.getErrCode();
                if(this$errCode == null) {
                    if(other$errCode != null) {
                        return false;
                    }
                } else if(!this$errCode.equals(other$errCode)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof JsonResult;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        int result = result * 59 + (this.isSuccess()?79:97);
        Object $data = this.getData();
        result = result * 59 + ($data == null?43:$data.hashCode());
        Object $msg = this.getMsg();
        result = result * 59 + ($msg == null?43:$msg.hashCode());
        Object $errCode = this.getErrCode();
        result = result * 59 + ($errCode == null?43:$errCode.hashCode());
        return result;
    }

    public String toString() {
        return "JsonResult(success=" + this.isSuccess() + ", data=" + this.getData() + ", msg=" + this.getMsg() + ", errCode=" + this.getErrCode() + ")";
    }
}

posted @ 2020-07-22 14:34  荭丶尘  阅读(555)  评论(0编辑  收藏  举报