Java项目构建基础:统一结果,统一异常,统一日志(好文推荐)
/**
* 接口响应状态码
*/
public enum ResponseCodeStatus {
SUCCESS("0","成功"),
FAIL("1","失败"),
OVERTIME("2","超时"),
WARNING("3","警告"),
UNAUTHENTICATED("4","无权限")
;
private String code;
private String msg;
ResponseCodeStatus(String code, String msg){
this.code = code;
this.msg = msg;
}
@Override
public String toString(){
return code;
}
public String getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
import java.io.Serializable;
import java.sql.SQLException;
/**
* 返回前端的判断实体
*
*/
public class ResponseState implements Serializable{
private String code;
private Object data;
private String msg;
public ResponseState(){}
public ResponseState(ResponseCodeStatus status, Object data, String msg){
this.code = status.getCode();
this.data = data;
this.msg = msg;
}
/**
* 响应信息
* @return ResponseState
*/
public static ResponseState ofResult(ResponseCodeStatus code, Object data, String msg) {
return new ResponseState(code,data,msg);
}
/**
* 成功只返回状态
* @return ResponseState
*/
public static ResponseState ofSuccess() {
return new ResponseState(ResponseCodeStatus.SUCCESS,"SUCCESS","SUCCESS");
}
/**
* 成功返回数据
* @return ResponseState
*/
public static ResponseState ofSuccess(Object data) {
return new ResponseState(ResponseCodeStatus.SUCCESS,data,null);
}
/**
* 成功返回数据
* @return ResponseState
*/
public static ResponseState ofSuccess(Object data,String msg) {
return new ResponseState(ResponseCodeStatus.SUCCESS,data,msg);
}
/**
* 警告返回数据
* @return ResponseState
*/
public static ResponseState ofWarning(Object data) {
return new ResponseState(ResponseCodeStatus.WARNING,data,null);
}
/**
* 警告返回数据
* @return ResponseState
*/
public static ResponseState ofWarning(Object data,String msg) {
return new ResponseState(ResponseCodeStatus.WARNING,data,msg);
}
/**
* 警告不返回数据
* @return ResponseState
*/
public static ResponseState ofWarning(String msg) {
return new ResponseState(ResponseCodeStatus.WARNING,null,msg);
}
/**
* 失败只返回状态
* @return ResponseState
*/
public static ResponseState ofFail() {
return new ResponseState(ResponseCodeStatus.FAIL,null,null);
}
/**
* 失败返回失败信息
* @return ResponseState
*/
public static ResponseState ofFail(String msg) {
return new ResponseState(ResponseCodeStatus.FAIL,null,msg);
}
/**
* session过期只返回状态
* @return ResponseState
*/
public static ResponseState ofOverTime() {
return new ResponseState(ResponseCodeStatus.OVERTIME,null,"session过期,请重新登录");
}
/**
* session过期只返回状态
* @return ResponseState
*/
public static ResponseState ofOverTime(Object data) {
return new ResponseState(ResponseCodeStatus.OVERTIME,data,"session过期,请重新登录");
}
public static ResponseState ofOverTime(String msg) {
return new ResponseState(ResponseCodeStatus.OVERTIME,null,msg);
}
public static ResponseState ofUnauthenticated(String msg) {
return new ResponseState(ResponseCodeStatus.UNAUTHENTICATED,null,msg);
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}