Solon

统一的渲染控制

</> markdown

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

Solon 特性之一:

可让控制器(一般定义控制器基类)实现 Render,从而接管控制器的渲染动作。

1、定义个接口基类,并实现渲染接口(比如:控制器基类)

简单点的渲染逻辑:

  1. 如果是异常,转为 Result 渲染输出
  2. 其它直接渲染输出
public class ControllerBase implements Render { //这是个基类,其它控制器在继承它
    @Override
    public void render(Object obj, Context ctx) throws Throwable {
        if (obj instanceof Throwable) {
            ctx.render(Result.failure(500));
        } else {
            ctx.render(obj);
        }
    }
}

复染点的渲染逻辑:

  1. 如果对象是null,跳过不管
  2. 如果是String,直接输出
  3. 如果是ModelAndView,直接渲染
  4. 如果是UapiCode,将其转为Result,再交给框架渲染
  5. 如果是Throwable,将其转为Result,再交给框架渲染
  6. 如果是其它非Result数据,将其转为Result,再交给框架渲染
//这个注解可继承,用于支持子类的验证
//
@Valid
public class ControllerBase 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));
        }
    }
}

2、接口基类应用示例

示例,白名单检测接口

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

@Controller
public class CMD_list_check extends ControllerBase {
    //此处的@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 ControllerBase {
    //此处的@NotEmpty验证,如果没通过会抛出UapiCode
    @NotEmpty("tag")
    @Mapping("/cfg/get/")
    public List<ConfigModel> cmd_exec(String tag) throws Throwable {
        return DbWaterCfgApi.getConfigByTag(tag);
    }
}

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

3、附属代码

  • UapiCode.java (用 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();
    }
}
  • UapiCodes.java(只是一个常量集合)
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");
}