枚举结果代码
public enum ResultCode {
SUCCESS(200, "请求成功"),
FAIL(400, "请求失败"),
TOKEN_INVALID(401, "Token非法");
private Integer code;
private String message;
ResultCode(Integer code, String message){
this.code = code;
this.message = message;
}
public Integer code(){ return this.code; }
public String message(){ return this.message; }
}
Response封装
public class Result {
private int code;
private String message;
private Object data;
public Object getData(){ return data; }
public void setData(Object data){ this.data = data; }
public void setCode(int code) { this.code = code; }
public int getCode() { return code; }
public void setMessage(String message){ this.message = message; }
public String getMessage() { return message; }
private void setResultCode(ResultCode resultCode){
this.code = resultCode.code();
this.message = resultCode.message();
}
public Result(){}
public static Result succuess(){
Result result = new Result();
result.setResultCode(ResultCode.SUCCESS);
return result;
}
public static Result succuess(Object data){
Result result = new Result();
result.setResultCode(ResultCode.SUCCESS);
result.setData(data);
return result;
}
public static Result fail() {
Result result = new Result();
result.setResultCode(ResultCode.FAIL);
return result;
}
public static Result fail(ResultCode resultCode) {
Result result = new Result();
result.setResultCode(resultCode);
return result;
}
public static Result fail(String message) {
Result result = new Result();
result.setCode(ResultCode.FAIL.code());
result.setMessage(message);
return result;
}
public static Result fail(Integer code, String message) {
Result result = new Result();
result.setCode(code);
result.setMessage(message);
return result;
}
}
测试
修改测试类
修改测试类.png image.png
网友评论