下面这个场景是特意为此文设计出来的,有点儿乱。但凡类似的场景。。。Solon 都可以给你一个so easy的支持。

Solon 特性之一:

可让控制器实现 Render,从而接管控制器的渲染动作。

1、定义个接口基类,并实现渲染接口

渲染逻辑如下:

  1. 如果对象是null,跳过不管
  2. 如果是String,直接输出
  3. 如果是ONode,做为Json输出
  4. 如果是UapiCode,将其转为Result,再序列化为Json输出
  5. 如果是Throwable,将其转为Result,再序列化为Json输出
  6. 如果是其它数据,直接序列化为Json输出

代码:

//这个注解可继承,用于支持子类的验证
//
@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 ONode) {
ctx.outputAsJson(((ONode) obj).toJson());
} else {
if (obj instanceof UapiCode) {
//此处是重点,把一些特别的类型进行标准化转换
//
UapiCode err = (UapiCode) obj;
obj = XResult.failure(err.getCode(), UapiCodes.getDescription(err));
} if (obj instanceof Throwable) {
//此处是重点,把异常进行标准化转换
//
Throwable err = (Throwable) obj;
obj = XResult.failure(err.getMessage());
} ctx.outputAsJson(ONode.stringify(obj));
}
}
}
}

2、接口基类应用示例

示例,白名单检测接口

此接口做个白名单检测。如果成功,则返加符串:OK

@Controller
public class CMD_run_whitelist_check extends UapiBase {
//此处的@NotEmpty验证,如果没通过会抛出UapiCode
@NotEmpty({"type", "value"})
@Mapping("/run/whitelist/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!");
}
}
}

示例,通知推送接口

此接口只能白名单里的IP方可调用。执行后返回:Result

//此处的@Whitelist验证,如果没通过会抛出UapiCode
@Whitelist
@Controller
public class CMD_run_push extends UapiBase {
//此处的@NotEmpty验证,如果没通过会抛出UapiCode
@NotEmpty({"msg", "target"})
@Mapping("/run/push/")
public Result cmd_exec(String msg, String target) throws Exception { List<String> list = new ArrayList<String>();
for (String str : target.split(",")) {
if (str.equals("@alarm")) {
List<String> mobiles = DbWaterCfgApi.getAlarmMobiles(); list.addAll(mobiles);
} else {
list.add(str);
}
} String rest = ProtocolHub.heihei.push(Config.water_service_name, list, msg); if (TextUtils.isEmpty(rest) == false) {
return Result.succeed(ONode.load(rest));
} else {
return Result.failure();
}
}
}

示例,配置获取接口

此接口返回一组配置,以ONode类型返回

//@Logging是个拦截器,会对请求输入进行记录
@Logging
//此处的@Whitelist验证,如果没通过会抛出UapiCode
@Whitelist
@Controller
public class CMD_cfg_get extends UapiBase {
//此处的@NotEmpty验证,如果没通过会抛出UapiCode
@NotEmpty("tag")
@Mapping("/cfg/get/")
public ONode cmd_exec(Context ctx, String tag) throws Throwable {
ONode nList = new ONode().asObject(); if (TextUtils.isEmpty(tag) == false) {
List<ConfigModel> list = DbWaterCfgApi.getConfigByTag(tag); Date def_time = new Date(); for (ConfigModel m1 : list) {
if (m1.update_fulltime == null) {
m1.update_fulltime = def_time;
} ONode n = nList.getNew(m1.key);
n.set("key", m1.key);
n.set("value", m1.value); if (m1.update_fulltime == null) {
n.set("lastModified", 0);
} else {
n.set("lastModified", m1.update_fulltime.getTime());
}
}
} return nList;
}
}

此文的渲染控制重点是对抛出来的UapiCode和Throwable,进行有效的控制并以统一的Result形态输出。对外接口开发时,还是效果可期的。当然,也可以用此特性干点别的什么事儿。

Solon Web 开发,十二、统一的渲染控制的更多相关文章

  1. Solon Web 开发,二、开发知识准备

    Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...

  2. Solon Web 开发,十四、与Spring、Jsr330的常用注解对比

    Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...

  3. Solon Web 开发

    Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...

  4. Solon Web 开发,一、开始

    Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...

  5. Solon Web 开发,四、请求上下文

    Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...

  6. Solon Web 开发,五、数据访问、事务与缓存应用

    Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...

  7. Solon Web 开发,六、过滤器、处理、拦截器

    Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...

  8. Solon Web 开发,七、视图模板与Mvc注解

    Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...

  9. Solon Web 开发,八、校验、及定制与扩展

    Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...

随机推荐

  1. LuoguP7870 「Wdoi-4」兔已着陆 题解

    Content 对一个栈执行如下操作: 1 l r:依次向栈里面弹入 \(l,l+1,\dots,r-1,r\). 2 k:依次从栈里面弹出 \(k\) 个数,并求出所有弹出的数的和. 数据范围:\( ...

  2. JAVA匹配html中所有img标签

    public static List<String> getImg(String htmlStr) { List<String> list = new ArrayList< ...

  3. mysql报错:You do not have the SUPER privilege and binary logging is enabled

    MySQL出现 You do not have the SUPER privilege and binary logging is enabled报错 解决方案: 1.用root用户登录:mysql ...

  4. 【LeetCode】802. Find Eventual Safe States 解题报告(Python)

    [LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

  5. Fast Matrix Operations(UVA)11992

    UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y ...

  6. python学习第四天:python基础(字符编码和乱码到底咋回事儿)

    字符编码 这得从字符编码开始说起: 字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题.因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理. 最早的计算机在设计时采 ...

  7. 手把手一步一步教你使用Java开发一个大型街机动作闯关类游戏01游戏窗口

    项目源码 项目源码 游戏配置信息类 Config.java 没什么解释的. package config; public class Config { public final static Stri ...

  8. Spring中的@Bean注解

    @Bean 基础概念 @Bean:Spring的@Bean注解用于告诉方法,产生一个Bean对象,然后这个Bean对象交给Spring管理.产生这个Bean对象的方法Spring只会调用一次,随后这个 ...

  9. 常见分布式唯一ID生成策略

    方法一: 用数据库的 auto_increment 来生成 优点: 此方法使用数据库原有的功能,所以相对简单 能够保证唯一性 能够保证递增性 id 之间的步长是固定且可自定义的 缺点: 可用性难以保证 ...

  10. CoGAN

    目录 概 主要内容 代码 Liu M., Tuzel O. Coupled Generative Adversarial Networks. NIPS, 2016. 概 用GAN和数据(从边缘分布中采 ...