验证中...
Languages: Java
Categories: 常用工具包
Latest update 2019-08-27 16:03
java返回通用结果类
Raw Copy
package com.common;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;
/**
* 通用结果类
* @author zys
* @ 2019年08月27日11:26:30
*/
public class ResultEntity<T> {
/**
* 返回正常,状态 0
*/
public static final int OK = 0;
/**
* 返回错误,状态1
*/
private static final int ERROR = 1;
/**
* 编码,0为正常,错误码1
*/
@ApiModelProperty("编码,0为正常,错误码1")
private int returncode;
/**
* 信息
*/
@ApiModelProperty("信息")
private String message;
/**
* 结果集
*/
@ApiModelProperty("结果集")
private T result;
/**
* 构造正常对象
* @param data
* @param <T>
* @return
*/
public static <T> ResultEntity<T> ok(T data){
ResultEntity<T> rn = new ResultEntity<>();
rn.returncode = OK;
rn.message = "OK";
rn.result = data;
return rn;
}
/**
* 重载无参正常返回对象
* @param <T>
* @return
*/
public static <T> ResultEntity<T> getSuccessResult(){
return ok(null);
}
/**
* 构造错误对象
* @param code
* @return
*/
public static <T> ResultEntity<T> getErrorResult(int code){
ResultEntity<T> rn = new ResultEntity<>();
rn.returncode = code;
rn.message = "发生异常!";
return rn;
}
/**
* @param message
* @return
*/
public static <T> ResultJson<T> getErrorResult(String message){
ResultEntity<T> rn = new ResultEntity<>();
rn.returncode = ERROR;
rn.message = StringUtils.defaultIfEmpty(message,"发生错误!");
return rn;
}
/**
* 构造错误对象
* @param code
* @param message
* @return
*/
public static <T> ResultEntity<T> getErrorResult(int code, String message){
ResultEntity<T> rn = new ResultEntity<>();
rn.returncode = code;
rn.message = StringUtils.defaultIfEmpty(message,"发生错误!");
return rn;
}
public static <T> ResultEntity<T> getErrorResult() {
return ResultEntity.getErrorResult(ERROR, "发生错误!");
}
public static <T> ResultEntity<T> getSuccessResult(int code, String message){
ResultEntity<T> rn = new ResultEntity<>();
rn.returncode = code;
rn.message = message;
rn.result = null;
return rn;
}
public static <T> ResultEntity<T> getSuccessResult(){
ResultEntity<T> rn = new ResultEntity<>();
rn.returncode = 0;
rn.message = "OK";
rn.result = null;
return rn;
}
public static <T> ResultEntity<T> ResultEntity(int code, String message, T data){
ResultEntity<T> rn = new ResultEntity<>();
rn.returncode = code;
rn.message = message;
rn.result = data;
return rn;
}
/**
* 获取描述
*
* @return message 描述
*/
public String getMessage() {
return this.message;
}
/**
* 设置描述
*
* @param message 描述
*/
public void setMessage(String message) {
this.message = message;
}
/**
* 获取数据对象
*
* @return data 数据对象
*/
public T getResult() {
return this.result;
}
/**
* 设置数据对象
*
* @param data 数据对象
*/
public void setResult(T data) {
this.result = data;
}
/**
*
* @return code 编码
*/
public int getReturncode() {
return this.returncode;
}
/**
*
* @param code 编码
*/
public void setReturncode(int code) {
this.returncode = code;
}
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search