接口返回消息封装类
2019.04.08 | 725阅读 | 0条评论 | java
package com.kd.modelapi.common;
import java.io.PrintWriter;
import java.io.StringWriter;
import com.kd.enu.ResultEnum;
/**
* 通用的对外返回结果字段
*/
public class ResponseVo {
private Integer status = ResultEnum.SUCCESS.getStatus(); // 返回状态 整型
private String result = ResultEnum.SUCCESS.getResult(); // 返回状态 字符串型
// 只有success,error
private String info; // 会信息,程序员自己抛出的异常信息
private Object data; // 返回数据 object
private Integer errorCode; // 错误码
private String stackInfo;// 异常信息
public ResponseVo() {
}
public ResponseVo(Integer status, String result, String info, Object data, Integer errorCode, String stackInfo) {
super();
this.status = status;
this.result = result;
this.info = info;
this.data = data;
this.errorCode = errorCode;
this.stackInfo = stackInfo;
}
public static ResponseVo success(){
return new ResponseVo(ResultEnum.SUCCESS.getStatus(), ResultEnum.SUCCESS.getResult(), ResultEnum.SUCCESS.getInfo(),null, null, null);
}
public static ResponseVo success(String info){
return new ResponseVo(ResultEnum.SUCCESS.getStatus(), ResultEnum.SUCCESS.getResult(), info,null, null, null);
}
public static ResponseVo success(String info,Object data){
return new ResponseVo(ResultEnum.SUCCESS.getStatus(), ResultEnum.SUCCESS.getResult(), info,data, null, null);
}
public static ResponseVo error(){
return new ResponseVo(ResultEnum.ERROR.getStatus(), ResultEnum.ERROR.getResult(), ResultEnum.ERROR.getInfo(),null, null, null);
}
public static ResponseVo error(String info){
return new ResponseVo(ResultEnum.ERROR.getStatus(), ResultEnum.ERROR.getResult(), info,null, null, null);
}
public static ResponseVo error(String info, String stackInfo){
return new ResponseVo(ResultEnum.ERROR.getStatus(), ResultEnum.ERROR.getResult(), info,null,null, stackInfo);
}
public static ResponseVo error(String info,Integer errorCode, String stackInfo){
return new ResponseVo(ResultEnum.ERROR.getStatus(), ResultEnum.ERROR.getResult(), info,null, errorCode, stackInfo);
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
// @MethodAnnotation(description = "success、error")
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
// @MethodAnnotation(description = "异常信息")
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
// @MethodAnnotation(description = "返回错误码")
public Integer getErrorCode() {
return errorCode;
}
public void setErrorCode(Integer errorCode) {
this.errorCode = errorCode;
}
public void setInfoStatus(String info, Integer state) {
this.info = info;
this.status = state;
if(ResultEnum.SUCCESS.getStatus().equals(state)){
this.result = "success";
}else{
this.result = "error";
}
}
public void setInfoStatus(Exception ex) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
this.info = sw.toString();
this.status = ResultEnum.ERROR.getStatus();
this.result = "error";
}
public ResponseVo setInfoStatus(String info, Integer errorCode, Integer status) {
this.errorCode = errorCode;
setInfoStatus(info, status);
return this;
}
public String getStackInfo() {
return stackInfo;
}
public void setStackInfo(String stackInfo) {
this.stackInfo = stackInfo;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("ResponseVo [status=");
builder.append(status);
builder.append(", result=");
builder.append(result);
builder.append(", info=");
builder.append(info);
builder.append(", data=");
builder.append(data);
builder.append(", errorCode=");
builder.append(errorCode);
builder.append(", stackInfo=");
builder.append(stackInfo);
builder.append("]");
return builder.toString();
}
}
返回码枚举package com.kd.enu;
/**
*
*
Title: ResultEnum
*
Description:返回结果码枚举
* @author mengjinyuan
* @date 2019年3月14日
*/
public enum ResultEnum {
SUCCESS(0,"成功","success"),
ERROR(-1,"错误","error"),
VERIFY_ERROR(3,"错误","error"),
CUSTOMER_ALEADAY(3,"错误","error");
private Integer status;
private String info;
private String result;
ResultEnum(Integer status,String info,String result){
this.status=status;
this.info=info;
this.result=result;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}
个人感觉这样设置比较的规范
打赏
微信扫一扫,打赏作者吧~