一、项目上出现对接时、应该定义统一结果信息,例如前端调用后端接口时
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