Solon v3.1.0

Action 结构图及两种注解处理

</> markdown

Action 即控制器下面的 @Mapping 函数(最终会适配成 Handler 实例,并注册到路由器)。内部处理过程:

  • 先进行 handler 层面的局部过滤(比如,局部的白名单过滤、流量拦截)
  • 之后是不同的 content-type 有不同的执行处理器(json、xml、protostuf)
  • 执行器处理器解析出参数后,会执行 MethodWrap 的方法(支持 method 级别的拦截)
  • 执行结果出来后,还可能按返回类型进行专门处理(比如 sse 和 rx 的处理)
  • 如果没有专门的返回类型处理,则进入通用渲染流程

1、相关接口

接口说明
-name()->String名字
-mapping()->Mapping映射
-method()->MethodWrap方法包装器
-controller()->BeanWrap控制类包装器
-produces()->String生产内容(主要用于文档生成)
-consumes()->String消费内容 (主要用于文档生成)

2、获取方式

Action action = ctx.action(); //可能为 null

3、两种重要的注解处理

Action 本质上是 Handler 和 Class Method 的结合。所以支持两种风格的注解和拦截处理:

@Around@Addition
注解对象Method(任何 Bean 可用)Action(仅 Web 控制器可用)
附加内容InterceptorFilter

a) Handler 的附加过滤处理(@Addition)

具体参考: 《@Addition 使用说明(AOP)》 。可以在 Method 参数转换之前执行(一般用做提前校验);也可以在 Method 执行并输出后执行。例如:

@Addition(WhitelistFilter.class)
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Whitelist { }

@Controller
public class DemoController{
    @Whitelist
    @Mapping("user/del")
    public void delUser(..){  }
}

b) Class Method 的包围拦截处理(@Around)

具体参考:《@Around 使用说明(AOP)》《切面与环绕拦截》

@Around(TranInterceptor.class)
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Tran {...}

@Controller
public class DemoController{
    @Tran
    @Mapping("user/del")
    public void delUser(..){  }
}

@Component
public class DemoService{
    @Tran
    public void delUser(..){  }
}