为什么要统一返回值
在我们做后端应用的时候,前后端分离的情况下,我们经常会定义一个数据格式,通常会包含code
,message
,data
这三个必不可少的信息来方便我们的交流,下面我们直接来看代码
ReturnVO
package indi.viyoung.viboot.util;
import java.util.Properties;
/**
* 统一定义返回类
*
* @author yangwei
* @since 2018/12/20
*/
public class ReturnVO {
private static final Properties properties = ReadPropertiesUtil.getProperties(System.getProperty("user.dir") + "/viboot-common/src/main/resources/response.properties");
/**
* 返回代码
*/
private String code;
/**
* 返回信息
*/
private String message;
/**
* 返回数据
*/
private Object data;
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
/**
* 默认构造,返回操作正确的返回代码和信息
*/
public ReturnVO() {
this.setCode(properties.getProperty(ReturnCode.SUCCESS.val()));
this.setMessage(properties.getProperty(ReturnCode.SUCCESS.msg()));
}
/**
* 构造一个返回特定代码的ReturnVO对象
* @param code
*/
public ReturnVO(ReturnCode code) {
this.setCode(properties.getProperty(code.val()));
this.setMessage(properties.getProperty