一、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;
}
}
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;
}
}
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;
}
}