当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
37 Star 55 Fork 17

helyho / Vestful
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

#####基于 Voovan 开发的通用 Restful 服务框架。旨在为广大开发者提供一个快速、稳定、功能丰富、自产文档的 Restful 框架。目的是在完成业务实现的同时,说明文档、接口服务等同时完成。这是一个 Voovan 项目的模块,需要在 Voovan WebServer 项目中添加该模块.

特点:

  • 仅仅通过两个注解就可以自动将方法暴露成 Restful 接口,并生成完善的Restful 接口说明文档。

  • 自动识别路径中的变量、常规请求变量、URLEncode以及MUTILPART提交变量。

    变量来源优先: 路径变量 > URLEncode以及MUTILPART提交变量 > 常规请求变量。
  • 支持 HTTP 协议中的方法以及任意的自定义 HTTP 方法。

  • 由于 HTTP 协议发送的请求为字符串,所以方法的参数类型目前支持常规的 Java基础类型CollectionMap自定义类型

  • 对方法的参数个数、返回值类型等没有要求,可以使用任意类型。如果方法返回的是一个自定义的对象,会自动转换成 JSON 字符串的形式返回。

  • 需要访问说明文档,请在对应的接口URL后增加!, 如接口路径为/test, 访问文档则通过/test!来访问。

  • 通过配置中的 params 节点可以向类中注入属性,注入属性使用的是属性对应的 set 方法。

  • 可以通过 js 直接在页面中操作后台 java 类,实现前后台编码无阻碍.

    对安全性的支持:

    • 1.支持类可访问控制,控制那些类可以被前台操作。
    • 2.支持类或者包的别名控制,对某些不想暴露类路径或者类的完全现定名的可以通过别名的方式隐藏类。
    • 3.前端实例化的对象的存活时间控制,防止 OOM。使用对象的方法等操作会默认自动刷新存活时间。

#####接口服务演示:http://vestful.voovan.org/vestful/test! #####接口方法演示:http://vestful.voovan.org/vestful/test/testWithReturnObject! #####Call java 类演示:http://vestful.voovan.org

###一、准备Vestful运行环境

  • ####部署 VoovanServer:

下载 最新版本的VoovanServer, 并修改配置文件web.json

    //配置文件:conf/web.json
    "Modules": [
        {
            "Name": "Vestful 模块",                                      //模块名称
            "Path": "/vestful",                                          //模块路径
            "ClassName": "org.voovan.vestful.RestfulModule"             //模块处理器
        }
    ]
  • ####服务配置: 服务配置请参照 Voovan 的说明文档,主要配置/conf/web.json 文件即可。

###二、编写业务类 ####注解说明:

  • Restful注解

    • 使用方式: 在方法上使用。
    • 参数:
      • method / HTTP 请求的方法类型。例如: GET、POST、PUT 等等。
      • desc / 关于方法的描述信息。
  • Param注解

    • 使用方式: 在方法的参数上使用。
    • 参数:
      • name: 参数名称。
      • desc: 关于参数的描述信息。
  • 注解使用举例:

    //类:org.voovan.test.vestful.TestClass.java

    //方法注解
    @Restful(method="LOCK", desc="This is a method description. test float param")
    public static TestResult testWithReturnObject(
                                 @Param(name="test1",desc="Param name is test1, type is String") //参数注解
                                         String test1,
                                 @Param(name="test2",desc="Param name is test2. type is int") //参数注解
                                         int test2){
        return new TestResult(test1,test2);
    }

###三、修改配置文件 ####修改 conf/vestful.json ####增加以下内容到配置文件中:

//配置文件:conf/vestful.json
[
    {
      "name":"test",
      "route":"/test",
      "classPath":"org.voovan.test.restful.TestClass", //上一步中变写的业务类
      "desc":"this is a test interface."
    }
]
  • name: 接口名称
  • route: 服务搜寻的路由,如果没有这个节点,则默认值为 classPath 的类名,例如:org.voovan.restful.TestClass, 则默认 route 为/TestClass
  • classPath: 提供接口的类说明
  • desc: 接口描述信息

####使用举例: 测试代码请参照:org.voovan.test.vestful.TestClass.java


####说明文档解释: 类说明及测试页面: http://x.x.x.x/vestful/test!

  • Vestful 是在 web.json 中配置的模块访问路径.
  • test 是在 vestful.json 中配置的当前类的route参数.

类中某个方法的说明及测试页面: http://x.x.x.x/vestful/test/testWithObject!

  • Vestful 是在 web.json 中配置的模块访问路径.
  • test 是在 vestful.json 中配置的当前类的route参数
  • testWithObject 类中的方法名

以上是基本的 Restful 服务的提供方式,下面将会介绍如何在页面的 JS 中操作后台的 java 对象.


###四、在页面的 JS 中操作后台的 java 对象 ####修改 conf/vestful.json ####增加以下内容到配置文件中::

//配置文件:conf/vestful.json
[
    "name": "DirectObject",
    "route": "DirectObject",
    "classPath": "org.voovan.vestful.entity.DirectObject",
    "desc": "Restful API for DirectObject",
    "params": {
      //这里是访问这个服务的完整路径(web.json中配置的Vestful模块路径+ 当前服务的类路径)
      //这里 web.json 中配置的是"/",当前类访问路径配置的是"DirectObject",所以通过/DirectObject来访问
      "route":"DirectObject",
      //这里控制 class 是否能够被前端的 js 调用
      //只有这classControl这个节点里的 class 才可以被前端 js 调用
      "classControl": [
        "java.util.ArrayList",
        "org.voovan.test.vestful.*"
      ],
      //定义类的别名
      "alias":{
        "OVTV": "org.voovan.test.vestful.TestClass"
      },
      //对象存活时间控制,单位:秒
      "objectAliveTime":60
    }
]

在页面中引用 java 类到页面的 js 上下问中:

<script lang="javascript" src="/DirectObject/genScript/java.util.ArrayList"></script>

页面引入 js 的路径为conf/vestful.json配置文件中"org.voovan.vestful.entity.DirectObject"类的 params 参数列表中route属性中的路径。 路径后增加genScript来代表后台动态生成脚本。

例如: 例子中的配置为:"route":"DirectObject" 那么页面引入的 js 路径为:/DirectObject/genScript/类完全限定名或者别名即可将对象引入到页面中。


在页面中使用 Java 类:

//具体内容请参照 WEBAPP/test.html
<script lang="javascript" >
   arraylist1 = new ArrayList();
   arraylist1.add("aaaa");

   arraylist2 = new ArrayList();
   arraylist2.add("bbbb");

   //支持在前台构造的 java 对象的相互引用
   arraylist1.addAll(arraylist2);

   //手工释放对象,如果不手工释放则根据配置文件里的存活时常进行释放
   arraylist1.release();
   arraylist2.release();
<script/>

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: You must give any other recipients of the Work or Derivative Works a copy of this License; and You must cause any modified files to carry prominent notices stating that You changed the files; and You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2015 heting Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

基于 Voovan 开发的通用 Restful 服务框架. 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/helyho/Vestful.git
git@gitee.com:helyho/Vestful.git
helyho
Vestful
Vestful
master

搜索帮助