zoukankan      html  css  js  c++  java
  • 自定义类库:接口交互时,可以返回统一结果信息

    一、项目上出现对接时、应该定义统一结果信息,例如前端调用后端接口时

    package com.moy.site.test.bean;
    
    import java.io.Serializable;
    
    /**
     * Description:装载数据Bean<br/>
     * Date:2017/10/20 <br/>
     * Email:moy25@foxmail.com <br/>
     *
     * @author YeXiangYang
     */
    public final class ResultBean<T> implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        public static final int SUCCESS_CODE = 200;
        public static final int FAIL_CODE = 404;
        public static final int ERROR_CODE = 500;
    
        public static final String SUCCESS = "success";
        public static final String FAIL = "fail";
        public static final String ERROR = "error";
    
        // 数据状态码
        private int code;
        // 数据附加信息
        private String message;
        // 数据
        private T data;
    
        private ResultBean() {
            super();
        }
    
        private ResultBean(int code, String message, T data) {
            super();
            this.code = code;
            this.message = message;
            this.data = data;
        }
    
        public static <E> ResultBean<E> success() {
            return success(ResultBean.SUCCESS, null);
        }
    
        public static <E> ResultBean<E> success(E data) {
            return success(ResultBean.SUCCESS, data);
        }
    
        public static <E> ResultBean<E> success(String message, E data) {
            return new ResultBean<>(ResultBean.SUCCESS_CODE, message, data);
        }
    
        public static <E> ResultBean<E> fail() {
            return fail(ResultBean.FAIL, null);
        }
    
        public static <E> ResultBean<E> fail(E data) {
            return fail(ResultBean.FAIL, data);
        }
    
        public static <E> ResultBean<E> fail(String message, E data) {
            return new ResultBean<>(ResultBean.FAIL_CODE, message, data);
        }
    
        public static <E> ResultBean<E> error() {
            return error(ResultBean.ERROR, null);
        }
    
        public static <E> ResultBean<E> error(E data) {
            return error(ResultBean.ERROR, data);
        }
    
        public static <E> ResultBean<E> error(String message, E data) {
            return new ResultBean<>(ResultBean.ERROR_CODE, message, data);
        }
    
        public boolean isSuccess() {
            return ResultBean.SUCCESS_CODE == this.getCode();
        }
    
        public int getCode() {
            return code;
        }
    
        public String getMessage() {
            return message;
        }
    
        public T getData() {
            return data;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        public void setData(T data) {
            this.data = data;
        }
    
        @Override
        public String toString() {
            return "Result [code=" + code + ", message=" + message + ", data=" + data + "]";
        }
    
    }

    yexiangyang

    moyyexy@gmail.com


     

  • 相关阅读:
    一周精彩内容分享(第 5 期):货拉拉悲剧的背后
    关于 HTTP 后端人员需要了解的 20+ 图片!
    百度地图午夜暗蓝风格
    百度地图开发自定义信息窗口openInfoWindow样式
    百度地图infowindow上添加自定义点击事件
    js显示当前日期时间和星期几
    iview 树形异步加载,首次加载子节点不能选择,点击父节点后才可以选择
    js 修改属性名和值。并只保留需要的属性
    css 条形百分比
    echarts 3d饼图
  • 原文地址:https://www.cnblogs.com/moy25/p/8610618.html
Copyright © 2011-2022 走看看