diff --git a/plugin_rest.html b/plugin_rest.html index a69cf0a03..fe39a87d8 100644 --- a/plugin_rest.html +++ b/plugin_rest.html @@ -125,7 +125,7 @@

/**
  * 只能依附在Service实现类的public方法上
- * value默认为"/" + Service的类名去掉Service字样的小写字符串 (如HelloService,的默认路径为/hello)。
+ * value默认为"/" + Service的类名去掉Service及后面字样的小写字符串 (如HelloService,的默认路径为/hello)。
  */
 @Target({METHOD})
 public @interface RestMapping {
@@ -160,11 +160,10 @@
 }
                 
-

        REST的设置方式有两大种: 一种是采用默认REST注解,一种是显式的设置。
第一种方式需要开发者对Service中的方法命名需要遵循一定规则, 如下范例,模块为Hello,Service类命名为HelloService,增删改查的方法采用createHello、deleteHello、updateHello、queryHello或其他xxxHello命名。REST插件加载任何没有标记@RestController、@RestMapping、@RestParam 的Service将按照一定规则生成默认值:
-         1、@RestController.value() 默认值为Service类名的小写化并去掉service字样, 视为模块名
+         1、@RestController.value() 默认值为Service类名的小写化并去掉service及后面字样, 视为模块名
        2、@RestMapping.name() 默认值为Service的方法名小写化并去掉模块名字样
        2、@RestParam.value() 如果方法名以find、delete开头且方法的参数只有一个且参数类型是基本数据类型或String,则默认值为"#";若使用Java 8中带上 -parameters 编译项的新特性,默认值为参数名, 若没使用新特性则采用bean、bean2、bean3...的命名规则。

@@ -218,7 +217,201 @@ }

-         根据默认命名规则可以看出,以上范例生成的RestServlet与去掉所有@RestController、@RestMapping、@RestParam后的Service生成的是完全相同的。
+         根据默认命名规则可以看出,以上范例生成的RestServlet与去掉所有@RestController、@RestMapping、@RestParam后的Service生成的是完全相同的。 REST插件根据Service会动态生成HttpServlet,以上范例生成的HttpServlet如下:
+

+ +
@WebServlet(value = {"/hello/*"}, repair = true)
+public class _DynHelloRestServlet extends SimpleRestServlet {
+
+    @Resource
+    private HelloService _service;
+
+    @AuthIgnore
+    @WebAction(url = "/hello/create")
+    public void create(HttpRequest req, HttpResponse resp) throws IOException {
+        HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean");
+        UserInfo user = currentUser(req);
+        RetResult<HelloEntity> result = _service.createHello(user, bean);
+        sendRetResult(resp, result);
+    }
+
+    @AuthIgnore
+    @WebAction(url = "/hello/delete/")
+    public void delete(HttpRequest req, HttpResponse resp) throws IOException {
+        int id = Integer.parseInt(req.getRequstURILastPath());
+        _service.deleteHello(id);
+        sendRetResult(resp, RetResult.SUCCESS);
+    }
+
+    @AuthIgnore
+    @WebAction(url = "/hello/update")
+    public void update(HttpRequest req, HttpResponse resp) throws IOException {
+        HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean");
+        _service.updateHello(bean);
+        sendRetResult(resp, RetResult.SUCCESS);
+    }
+
+    @AuthIgnore
+    @WebAction(url = "/hello/query")
+    public void query(HttpRequest req, HttpResponse resp) throws IOException {
+        HelloBean bean = req.getJsonParameter(HelloBean.class, "bean");
+        Flipper flipper = findFlipper(req);
+        Sheet<HelloEntity> result = _service.queryHello(bean, flipper);
+        resp.finishJson(result);
+    }
+
+    @AuthIgnore
+    @WebAction(url = "/hello/find/")
+    public void find(HttpRequest req, HttpResponse resp) throws IOException {
+        int id = Integer.parseInt(req.getRequstURILastPath());
+        HelloEntity bean = _service.findHello(id);
+        resp.finishJson(bean);
+    }
+}
+                
+ +

+         REST的第二种设置方式是显式的设置, 看如下范例:
+

+
/**
+ * 类说明:
+ * Flipper : Source组件中的翻页对象
+ * UserInfo :当前用户类
+ * HelloEntity: Hello模块的实体类
+ * HelloBean: Hellow模块实现FilterBean的过滤Bean类
+ *
+ */
+public class HelloService implements Service {
+
+    @Resource
+    private DataSource source;
+
+    //增加记录
+    public RetResult<HelloEntity> createHello(UserInfo info, HelloEntity entity) {
+        entity.setCreator(info == null ? 0 : info.getUserid()); //设置当前用户ID
+        entity.setCreatetime(System.currentTimeMillis());
+        source.insert(entity);
+        return new RetResult<>(entity);
+    }
+
+    //删除记录
+    public void deleteHello(int id) { //通过 /hello/delete/1234 删除对象
+        source.delete(HelloEntity.class, id);
+    }
+
+    //修改记录
+    public void updateHello(HelloEntity entity) { //通过 /hello/update?bean={...} 修改对象
+        entity.setUpdatetime(System.currentTimeMillis());
+        source.update(entity);
+    }
+
+    //修改记录
+    @RestMapping(name = "partupdate")  //不能使用updatepart,因为存在update,是updatepart的开头部分,不符合BasedHttpServlet的WebAction规则
+    public void updateHello(HelloEntity entity, @RestParam("cols") String[] columns) { //通过 /hello/update?bean={...} 修改对象
+        entity.setUpdatetime(System.currentTimeMillis());
+        source.updateColumns(entity, columns);
+    }
+    
+    //查询Sheet列表
+    public Sheet<HelloEntity> queryHello(HelloBean bean, Flipper flipper) { //通过 /hello/query/start:0/size:20?bean={...} 查询Sheet列表
+        return source.querySheet(HelloEntity.class, flipper, bean);
+    }
+
+    //查询List列表
+    @RestMapping(name = "list")
+    public List<HelloEntity> queryHello(HelloBean bean) { //通过 /hello/list?bean={...} 查询List列表
+        return source.queryList(HelloEntity.class, bean);
+    }
+
+    //查询单个
+    @RestMapping(name = "find")
+    @RestMapping(name = "jsfind", jsvar = "varhello")
+    public HelloEntity findHello(@RestParam("#") int id) {  //通过 /hello/find/1234 查询对象
+        return source.find(HelloEntity.class, id);
+    }
+}
+                
+ +

+         转换为RestHttpServlet如下:
+

+
@WebServlet(value = {"/hello/*"}, repair = true)
+public class _DynHelloRestServlet extends SimpleRestServlet {
+
+    @Resource
+    private HelloService _service;
+
+    @AuthIgnore
+    @WebAction(url = "/hello/create")
+    public void create(HttpRequest req, HttpResponse resp) throws IOException {
+        HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean");
+        UserInfo user = currentUser(req);
+        RetResult<HelloEntity> result = _service.createHello(user, bean);
+        sendRetResult(resp, result);
+    }
+
+    @AuthIgnore
+    @WebAction(url = "/hello/delete/")
+    public void delete(HttpRequest req, HttpResponse resp) throws IOException {
+        int id = Integer.parseInt(req.getRequstURILastPath());
+        _service.deleteHello(id);
+        sendRetResult(resp, RetResult.SUCCESS);
+    }
+
+    @AuthIgnore
+    @WebAction(url = "/hello/update")
+    public void update(HttpRequest req, HttpResponse resp) throws IOException {
+        HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean");
+        _service.updateHello(bean);
+        sendRetResult(resp, RetResult.SUCCESS);
+    }
+
+    @AuthIgnore
+    @WebAction(url = "/hello/partupdate")
+    public void partupdate(HttpRequest req, HttpResponse resp) throws IOException {
+        HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean");
+        String[] cols = req.getJsonParameter(String[].class, "cols");
+        _service.updateHello(bean, cols);
+        sendRetResult(resp, RetResult.SUCCESS);
+    }
+
+    @AuthIgnore
+    @WebAction(url = "/hello/query")
+    public void query(HttpRequest req, HttpResponse resp) throws IOException {
+        HelloBean bean = req.getJsonParameter(HelloBean.class, "bean");
+        Flipper flipper = findFlipper(req);
+        Sheet<HelloEntity> result = _service.queryHello(bean, flipper);
+        resp.finishJson(result);
+    }
+
+    @AuthIgnore
+    @WebAction(url = "/hello/list")
+    public void list(HttpRequest req, HttpResponse resp) throws IOException {
+        HelloBean bean = req.getJsonParameter(HelloBean.class, "bean");
+        List<HelloEntity> result = _service.queryHello(bean);
+        resp.finishJson(result);
+    }
+
+    @AuthIgnore
+    @WebAction(url = "/hello/find/")
+    public void find(HttpRequest req, HttpResponse resp) throws IOException {
+        int id = Integer.parseInt(req.getRequstURILastPath());
+        HelloEntity bean = _service.findHello(id);
+        resp.finishJson(bean);
+    }
+
+    @AuthIgnore
+    @WebAction(url = "/hello/jsfind/")
+    public void jsfind(HttpRequest req, HttpResponse resp) throws IOException {
+        int id = Integer.parseInt(req.getRequstURILastPath());
+        HelloEntity bean = _service.findHello(id);
+        sendJsResult(resp, "varhello", bean);
+    }
+}
+                
+ +

+         REST插件让开发者省去了编写HttpServlet过程,让开发更加敏捷。