• 自定义类库:接口交互时,可以返回统一结果信息


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

    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


     

  • 相关阅读:
    JEECG与帆软报表集成
    各种数据库的锁表和解锁操作
    sql server数据库查询超时报错
    java项目部署后的文件路径获取
    js解决跨站点脚本编制问题
    java递归算法实现拼装树形JSON数据
    FreeMarker中的list集合前后台代码
    去除list集合中重复项的几种方法
    Java中Properties类的操作
    mysql 容灾 灾备 备份
  • 原文地址:https://www.cnblogs.com/moy25/p/8610618.html
Copyright © 2020-2023  润新知