Action参数注入

与SpringMVC类似,Action 参数注入是指为 action 方法传入参数,可以省去 getPara(...) 代码直接获得参数值,以下是代码示例:

@Action
public class UserAction {
    /**
     * 获取最原始的HttpRequest和HttpResponse参数
     */
    @Request(value = "/search", method = Request.Method.GET)
    public RxIo<String> search(HttpRequest request, HttpResponse response) {
        return RxIo.just("Search Info:" + request.getHeaders());
    }

    /**
     * 基于Variable模板的参数注入,URL示例如下
     * http://127.0.0.1:8880/product/110/mengniu
     */
    @Request.Post("/product/{id}/{name}")
    public RxIo<String> product(@Variable("id") int id, @Variable("name") String name) {
        return RxIo.just("Product Info:" + id + "-" + name);
    }

    /**
     * 基于Param类的参数数据包装,像操作Map一样操作Param获取参数,URL示例如下
     * curl localhost/search/12/television
     */
    @Request("/search/{id}/{name}")
    public RxIo<String> search(Param info) {
        return RxIo.just("Search Info2:" + info.getString("name"));
    }

    /**
     * 基于Bean Entity的参数注入,将请求json转换为对象模型
     * curl -i -X POST -H "Content-type:application/json" \
     * -d '{"field1":"value1","field5":[{"name":"MySubName1","id":1},{"name":"MySubName2","id":2}],"field4":{"sub2":"MyName1","sub3":{"name":"MySubName2","id":2},"sub1":"MyTitle1"},"field3":10,"field2":"value2"}'\
     * http://localhost:8880/body
     */
    @Request("/body")
    public RxIo<String> abody(@Model MyBean bean) {
        return RxIo.emitter(new IoEmitter<String>() {
            @Override
            public String call() throws Exception {
                HttpRequest request = bean.getRequest();
                Map<String, String> headers = request.getHeaders();
                StringBuilder response = new StringBuilder();
                response.append("Hello,Reactor-" + bean + ";flow:" + bean.getFlow() + "\r\n");
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    String key = entry.getKey();
                    String value = entry.getValue();
                    response.append(key + ":" + value + "\r\n");
                }
                return response.toString();
            }
        });
    }
}

MyBean类定义如下

public class MyBean extends ModelParametric {
    @NotNull
    private String field1;

    @NotEmpty
    private String field2;

    @Number(require = true, min = 0, max = 12)
    private int field3;

    private Param field4;

    private List<MyOption> field5;

    public String getField1() {
        return field1;
    }

    public void setField1(String field1) {
        this.field1 = field1;
    }

    public String getField2() {
        return field2;
    }

    public void setField2(String field2) {
        this.field2 = field2;
    }

    public int getField3() {
        return field3;
    }

    public void setField3(int field3) {
        this.field3 = field3;
    }

    public Param getField4() {
        return field4;
    }

    public void setField4(Param field4) {
        this.field4 = field4;
    }

    public List<MyOption> getField5() {
        return field5;
    }

    public void setField5(List<MyOption> field5) {
        this.field5 = field5;
    }

    public static class MyOption {
        @Digits
        private int id;

        @NotEmpty
        private String name;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}