渲染逻辑如下:
代码:
//这个注解可继承,用于支持子类的验证
//
@Valid
public class UapiBase implements Render {
@Override
public void render(Object obj, Context ctx) throws Throwable {
if (obj == null) {
return;
}
if (obj instanceof String) {
//普通字符串,直接输出
ctx.output((String) obj);
} else if (obj instanceof ModelAndView) {
//视图模型,直接渲染
ctx.render(obj);
} else {
if (obj instanceof UapiCode) {
//此处是重点,把一些特别的类型进行标准化转换
//
UapiCode err = (UapiCode) obj;
obj = Result.failure(err.getCode(), err.getDescription());
} else if (obj instanceof Throwable) {
//此处是重点,把异常进行标准化转换
//
Throwable err = (Throwable) obj;
obj = Result.failure(err.getMessage());
} else if(obj instanceof Result == false){
//否则做为成功数据,格式化为 Result 结构
//
obj = Result.succeed(obj);
}
ctx.render(obj);
//或者调用特定接口直接输出:ctx.outputAsJson(JSON.toJson(obj));
}
}
}
此接口做个白名单检测。如果成功,则返加符串:OK
@Controller
public class CMD_list_check extends UapiBase {
//此处的@NotEmpty验证,如果没通过会抛出UapiCode
@NotEmpty({"type", "value"})
@Mapping("/list/check/")
public String cmd_exec(Context ctx, String type, String value) throws Exception {
String tags = ctx.param("tags", "");
if (tags.contains("client")) {
if (DbWaterCfgApi.whitelistIgnoreClient()) {
return "OK";
}
}
if (DbWaterCfgApi.isWhitelist(tags, type, value)) {
return ("OK");
} else {
return (value + ",not is whitelist!");
}
}
}
此接口返回一组配置,以 List 类型返回
//@Logging是个拦截器,会对请求输入进行记录
@Logging
//此处的@Whitelist验证,如果没通过会抛出UapiCode
@Whitelist
@Controller
public class CMD_cfg_get extends UapiBase {
//此处的@NotEmpty验证,如果没通过会抛出UapiCode
@NotEmpty("tag")
@Mapping("/cfg/get/")
public List<ConfigModel> cmd_exec(String tag) throws Throwable {
return DbWaterCfgApi.getConfigByTag(tag);
}
}
此文的渲染控制重点是对抛出来的 UapiCode 和 Throwable,进行有效的控制并以统一的 Result 形态输出。对外接口开发时,还是效果可期的。当然,也可以用此特性干点别的什么事儿。
public class UapiCode extends DataThrowable {
private int code = 0;
private String description = "";
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public UapiCode(int code) {
super(code + ""); //给异常系统用的
this.code = code;
}
public UapiCode(int code, String description) {
super(code + ": " + description);//给异常系统用的
this.code = code;
this.description = description;
}
public UapiCode(Throwable cause) {
super(Result.FAILURE_CODE + ": " + cause.getMessage(), cause);
this.description = cause.getMessage();
}
}
public class UapiCodes {
public static final UapiCode CODE_200 = new UapiCode(200, "Succeed");
public static final UapiCode CODE_400 = new UapiCode(400, "Unknown error");
public static final UapiCode CODE_4001011 = new UapiCode(4001011, "The api not exist");
public static final UapiCode CODE_4001012 = new UapiCode(4001012, "The request is not up to par");
public static final UapiCode CODE_4001013 = new UapiCode(4001013, "The signature error");
public static final UapiCode CODE_4001014(String names) {
return new UapiCode(4001014, names);
}
public static final UapiCode CODE_4001015 = new UapiCode(4001015, "Too many requests");
public static final UapiCode CODE_4001016 = new UapiCode(4001016, "The request is not in the whitelist");
public static final UapiCode CODE_4001017 = new UapiCode(4001017, "Request capacity exceeds limit");
public static final UapiCode CODE_4001018 = new UapiCode(4001018, "The request for encryption and decryption failed");
public static final UapiCode CODE_4001021 = new UapiCode(4001021, "Login is invalid or not logged in");
}
如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!
加入交流群
请使用微信扫一扫!