【JAVA】JAVA接口公共返回体ResponseData封装

一、JAVA接口公共返回体ResponseData封装,使用泛型的经典 例子

public class ResponseData<T> implements Serializable {

/**

*

*/

private static final long serialVersionUID = 7098362967623367826L;

/**

* 响应状态码

*/

private Integer code;

/**

* 响应信息

*/

private String message;

/**

* 响应对象

*/

private T data;

/** 是否加密返回参数*/

private boolean encrypt;

public Integer getCode() {

return code;

}

public void setCode(Integer code) {

this.code = code;

}

public String getMessage() {

return message;

}

public void setMessage(String message) {

if(getCode().toString().startsWith("40") || getCode().toString().startsWith("50")) {

this.message = "内部程序暂时出小差了";

}else {

this.message = message;

}

}

public T getData() {

return data;

}

public void setData(T data) {

this.data = data;

}

public static <T> ResponseData<T> success() {

return success(ResponseCode.SUCCESS);

}

public static <T> ResponseData<T> success(ResponseCode code) {

return success(code, null);

}

public static <T> ResponseData<T> success(T object) {

return success(ResponseCode.SUCCESS, object);

}

public static <T> ResponseData<T> success(ResponseCode code, T object) {

return success(code.getCode(), code.getMessage(), object);

}

public static <T> ResponseData<T> success(Integer code, String message, T data) {

ResponseData<T> response = new ResponseData<>();

response.setCode(code);

response.setMessage(message);

response.setData(data);

return response;

}

public static <T> ResponseData<T> error() {

return error(ResponseCode.FAILED);

}

public static <T> ResponseData<T> error(ResponseCode code) {

return error(code, null);

}

public static <T> ResponseData<T> error(String message) {

return error(ResponseCode.FAILED.getCode(), message, null);

}

public static <T> ResponseData<T> error(Integer code, String message) {

return error(code, message, null);

}

public static <T> ResponseData<T> error(ResponseCode code, T data) {

return error(code.getCode(), code.getMessage(), data);

}

public static <T> ResponseData<T> error(Integer code, String message, T data) {

ResponseData<T> response = new ResponseData<>();

response.setCode(code);

response.setMessage(message);

response.setData(data);

return response;

}

public boolean isEncrypt() {

return encrypt;

}

public void setEncrypt(boolean encrypt) {

this.encrypt = encrypt;

}

}

java 复制代码
public class ResponseData<T> implements Serializable {

    /**
	 * 
	 */
	private static final long serialVersionUID = 7098362967623367826L;

	/**
     * 响应状态码
     */
    private Integer code;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 响应对象
     */
    private T data;

    /** 是否加密返回参数*/
    private boolean encrypt;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
     
    	 if(getCode().toString().startsWith("40") ||  getCode().toString().startsWith("50")) {
    		 this.message = "内部程序暂时出小差了"; 
    	 }else {
    		 this.message = message; 
    	 }
    	
       
    }


    public T getData() {
        return data;
    }

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

    public static <T> ResponseData<T> success() {
        return success(ResponseCode.SUCCESS);
    }

    public static <T> ResponseData<T> success(ResponseCode code) {
        return success(code, null);
    }

    public static <T> ResponseData<T> success(T object) {
        return success(ResponseCode.SUCCESS, object);
    }

    public static <T> ResponseData<T> success(ResponseCode code, T object) {
        return success(code.getCode(), code.getMessage(), object);
    }

    public static <T> ResponseData<T> success(Integer code, String message, T data) {
        ResponseData<T> response = new ResponseData<>();
        response.setCode(code);
        response.setMessage(message);
        response.setData(data);
        return response;
    }

    public static <T> ResponseData<T> error() {
        return error(ResponseCode.FAILED);
    }

    public static <T> ResponseData<T> error(ResponseCode code) {
        return error(code, null);
    }

    public static <T> ResponseData<T> error(String message) {
        return error(ResponseCode.FAILED.getCode(), message, null);
    }

    public static <T> ResponseData<T> error(Integer code, String message) {
        return error(code, message, null);
    }

    public static <T> ResponseData<T> error(ResponseCode code, T data) {
        return error(code.getCode(), code.getMessage(), data);
    }

    public static <T> ResponseData<T> error(Integer code, String message, T data) {
        ResponseData<T> response = new ResponseData<>();
        response.setCode(code);
        response.setMessage(message);
        response.setData(data);
        return response;
    }


    public boolean isEncrypt() {
        return encrypt;
    }

    public void setEncrypt(boolean encrypt) {
        this.encrypt = encrypt;
    }
}

二、返回状态码封装ResponseCode,枚举类型

public enum ResponseCode {

/* 公共状态码 */

SUCCESS(200, "成功"),

FAILED(400, "失败"),

UNAUTHORIZED(401, "签名错误"),

NOT_FOUND(404, "此接口不存在"),

INTERNAL_SERVER_ERROR(500, "系统繁忙,请稍后再试"),

/* 参数错误:10001-19999 */

PARAM_IS_INVALID(10001, "参数无效"),

PARAM_IS_BLANK(10002, "参数为空"),

PARAM_TYPE_BIND_ERROR(10003, "参数类型错误"),

PARAM_NOT_COMPLETE(10004, "参数缺失"),

/* 用户错误:20001-29999*/

USER_NOT_LOGGED_IN(20001, "用户未登录"),

USER_LOGIN_ERROR(20002, "账号不存在或密码错误"),

USER_ACCOUNT_FORBIDDEN(20003, "账号已被禁用"),

USER_NOT_EXIST(20004, "用户不存在"),

USER_HAS_EXISTED(20005, "用户已存在"),

LOGIN_CREDENTIAL_EXISTED(20006, "凭证已存在"),

//微信授权 错误码

WEIXIN_ENCRYPT_ERROR(30001,"企业微信授权解密失败"),

/*权限相关:40001-49999 */

NO_PERMISS(40001,"无权限访问"),

IS_LIMIT_ACC(40002,"访问过于频繁限制访问"),

/* 业务错误:50001-59999 */

WX_QRCODE_NULL(50001,"微信二维码获取内容不可用"),

/* 业务错误:60001-69999 */

XSS_CODE_ERROR(60001,"存在xss 恶意攻击脚本"),

PASS_REPEAT_ERROR(60002,"密码 不能与 前三次相同,请更换新密码");

private Integer code;

private String message;

ResponseCode(int code, String message) {

this.code = code;

this.message = message;

}

public int getCode() {

return code;

}

public String getMessage() {

return message;

}

}

java 复制代码
public enum ResponseCode {

    /* 公共状态码 */
    SUCCESS(200, "成功"),
    FAILED(400, "失败"),
    UNAUTHORIZED(401, "签名错误"),
    NOT_FOUND(404, "此接口不存在"),
    INTERNAL_SERVER_ERROR(500, "系统繁忙,请稍后再试"),

    /* 参数错误:10001-19999 */
    PARAM_IS_INVALID(10001, "参数无效"),
    PARAM_IS_BLANK(10002, "参数为空"),
    PARAM_TYPE_BIND_ERROR(10003, "参数类型错误"),
    PARAM_NOT_COMPLETE(10004, "参数缺失"),

    /* 用户错误:20001-29999*/
    USER_NOT_LOGGED_IN(20001, "用户未登录"),
    USER_LOGIN_ERROR(20002, "账号不存在或密码错误"),
    USER_ACCOUNT_FORBIDDEN(20003, "账号已被禁用"),
    USER_NOT_EXIST(20004, "用户不存在"),
    USER_HAS_EXISTED(20005, "用户已存在"),
    LOGIN_CREDENTIAL_EXISTED(20006, "凭证已存在"),

    //微信授权 错误码
    WEIXIN_ENCRYPT_ERROR(30001,"企业微信授权解密失败"),
    
	
	/*权限相关:40001-49999 */
	NO_PERMISS(40001,"无权限访问"),
	IS_LIMIT_ACC(40002,"访问过于频繁限制访问"),

    /* 业务错误:50001-59999 */
	WX_QRCODE_NULL(50001,"微信二维码获取内容不可用"),

    /* 业务错误:60001-69999 */
    XSS_CODE_ERROR(60001,"存在xss 恶意攻击脚本"),
	
	PASS_REPEAT_ERROR(60002,"密码 不能与 前三次相同,请更换新密码");
	

    private Integer code;

    private String message;

    ResponseCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
}
相关推荐
2401_857439691 小时前
SSM 架构下 Vue 电脑测评系统:为电脑性能评估赋能
开发语言·php
SoraLuna1 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
xlsw_2 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
神仙别闹2 小时前
基于java的改良版超级玛丽小游戏
java
Dream_Snowar3 小时前
速通Python 第三节
开发语言·python
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭3 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫3 小时前
泛型(2)
java
超爱吃士力架3 小时前
邀请逻辑
java·linux·后端
南宫生3 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石3 小时前
12/21java基础
java