最近公司把各种笔记都给屏蔽了,无奈只好过来写博客来记录一下,方便下次使用…
返回参数实体类
public class Result<T> {
/**
* @Fields code : 返回状态码
*/
private String code;
/**
* @Fields msg : 返回消息描述
*/
private String msg;
/**
* @Fields data : 返回实体数据
*/
private T data;
/**
* <p>Title: </p>
* <p>Description: 异常返回时的返回构造描述</p>
*
* @param code
* @param msg
*/
public Result(String code, String msg) {
this.code = code;
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
枚举类
public enum ResultEnum {
UNKNOWN_ERROR("-1", "未知错误"),
SUCCESS("200", "成功"),
PARAM_ERROR("201", "参数不合法"),
DATABASE_ERROR("202", "数据库异常");
private String code;
private String message;
ResultEnum(String code, String message) {
this.code = code;
this.message = message;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
调用Utils
import com.vivo.nids.entity.respdto.Result;
import com.vivo.nids.entity.respdto.ResultEnum;
/**
* @author 72038611
*/
public class ResultUtil {
/**
* @param @param object
* @param @return 设定文件
* @return Result 返回类型
* @Title: success
* @Description: 成功有返回
*/
public static Result success(Object object) {
Result result = new Result(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage());
result.setData(object);
return result;
}
/**
* @param @return 设定文件
* @return Result 返回类型
* @Title: success
* @Description: 成功无返回
*/
public static Result success() {
return ResultUtil.success(null);
}
/**
* @param @param object
* @param @return 设定文件
* @return Result 返回类型
* @Title: success
* @Description: 参数不合法返回
*/
public static Result paramError(Object object) {
Result result = new Result(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
result.setData(object);
return result;
}
/**
* @return Result 返回类型
* @Title: paramError
* @Description: 参数不合法返回
*/
public static Result paramError() {
return new Result(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
}
/**
* 返回自定义状态码及msg
* @param code
* @param msg
* @return
*/
public static Result customResultParam(String code, String msg) {
return new Result(code, msg);
}
/**
* @return Result 返回类型
* @Title: paramError
* @Description: 数据库错误
*/
public static Result dataBaseError() {
return new Result(ResultEnum.DATABASE_ERROR.getCode(), ResultEnum.DATABASE_ERROR.getMessage());
}
/**
* @param @param code
* @param @param msg
* @param @return 设定文件
* @return Result 返回类型, 错误返回时状态码为 9999
* @Title: error
* @Description: 失败方法返回
*/
public static Result error(String code, String msg) {
Result result = new Result(code, msg);
return result;
}
}