40 Star 90 Fork 44

Dreampie / Resty

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Resty 一款极简的restful轻量级的web框架

更新说明


  • feature/20170203 强化Stringer工具和让日志支持彩色输出方便开发者调试 @t-baby

Join the chat at https://gitter.im/Dreampie/Resty Issue Stats Issue Stats 开发文档

如果你还不是很了解restful,或者认为restful只是一种规范不具有实际意义,推荐一篇osc两年前的文章:RESTful API 设计最佳实践 和 Infoq的一篇极其理论的文章 理解本真的REST架构风格 虽然有点老,介绍的也很简单,大家权当了解,restful的更多好处,还请google

拥有jfinal/activejdbc一样的activerecord的简洁设计,使用更简单的restful框架

restful的api设计,是作为restful的服务端最佳选择(使用场景:客户端和服务端解藕,用于对静态的html客户端(mvvm等),ios,andriod等提供服务端的api接口)

Java开发指南:Java style guide

Api设计指南:Http api design

Resty例子: resty-samples(纯接口) resty-demo(带界面)

如果你在考虑前后端分离方案,推荐resty+vuejs,https://github.com/Dreampie/vuejs2-demo

开发群: 极简Restful框架 - Resty

其他开发者贡献的插件:Beetl扩展(大鹏) Shiro扩展(zhoulieqing) MongoPlugin(T-baby)

有兴趣一起维护该框架的,可以联系我,进入合作开发

规范:提前说明功能,新建分支 feature/日期 功能 fix/日期 修复 在readme里添加一个TODO list描述

  • feature/20161228 a task list item done @Dreampie
  • feature/20161229 a task list item todo @Dreampie

注意代码2格缩进,最后所有合作者一起代码review,合格之后合并到master

maven使用方式:

  1. 添加依赖包
<dependency>
    <groupId>cn.dreampie</groupId>
    <artifactId>resty-route</artifactId>
    <version>1.3.1.SNAPSHOT</version>
</dependency>

2.如果使用带有SNAPSHOT后缀的包,请添加该仓库

<repositories>
    <repository>
      <id>oss-snapshots</id>
      <url>https://oss.sonatype.org/content/repositories/snapshots</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
  </repositories>

一、独有优点:

重大更新:

1.3.0更新内容: 使用jetty作为嵌入式热加载默认实现(只要java文件进行编译就会重新加载),resty-captcha验证码功能...

1.2.0更新内容:使用header来控制api版本,基于数据源的读写分离,更简单的tableSetting.详情查看

1.1.0版本重大更新:快速接入spring,缓存,加密,header,XForwardedSupports等,详情查看

Record的时代已经到来,你完全不用使用任何的model来执行你的数据

//创建record的执行器  针对sec_user表 并开启缓存
Record recordDAO = new Record("sec_user");
//使用当前数据源和表数据 new一个对象来保存数据
recordDAO.reNew().set("属性", "值").save();
Record r1 = recordDAO.reNew().set("属性", "值");
Record r2 = recordDAO.reNew().set("属性", "值");
//批量保存
recordDAO.save(r1, r2);
//更新
r2.set("属性", "值").update()
//查询全部
List<Record> records = recordDAO.findAll();
//条件查询
recordDAO.findBy(where,paras)
//分页查询
Page<Record> records = recordDAO.paginateAll();
//根据id删除
recordDAO.deleteById("1");

//本次查询放弃使用cache 
recordDAO.unCache().findBy(where,paras);
//把record的数据源切换到dsmName数据源上
recordDAO.useDS(dsmName).findBy(where,paras);

//等等,完全摆脱model,实现快速操作数据

Model支持动态切换数据源和本次查询放弃使用cache

User dao=new User();
//本次查询放弃使用cache 
dao.unCache().findBy(where,paras);
//把model的数据源切换到dsmName数据源上
dao.useDS(dsmName).findBy(where,paras);

//数据库和全局参数配置移植到application.properties 详情参看resty-example

#not must auto load
app.encoding=UTF-8
app.devEnable=true
app.showRoute=false
app.cacheEnabled=true
#默认使用ehcacheProvider
#app.cacheProvider=cn.dreampie.cache.redis.RedisProvider

##druid plugin auto load
db.default.url=jdbc:mysql://127.0.0.1/example?useUnicode=true&characterEncoding=UTF-8
db.default.user=dev
db.default.password=dev1010
db.default.dialect=mysql

#c3p0配置
c3p0.default.minPoolSize=3
c3p0.default.maxPoolSize=20

#druid配置
#druid.default.initialSize=10
#druid.default.maxPoolPreparedStatementPerConnectionSize=20
#druid.default.timeBetweenConnectErrorMillis=1000
#druid.default.filters=slf4j,stat,wall

#flyway database migration auto load
flyway.default.valid.clean=true
flyway.default.migration.auto=true
flyway.default.migration.initOnMigrate=true


db.demo.url=jdbc:mysql://127.0.0.1/demo?useUnicode=true&characterEncoding=UTF-8
db.demo.user=dev
db.demo.password=dev1010
db.demo.dialect=mysql
#druid
druid.demo.initialSize=10
druid.demo.maxPoolPreparedStatementPerConnectionSize=20
druid.demo.timeBetweenConnectErrorMillis=1000
druid.demo.filters=slf4j,stat,wall
#flyway
flyway.demo.valid.clean=true
flyway.demo.migration.auto=true
flyway.demo.migration.initOnMigrate=true



//数据库的配置精简  自动从文件读取参数  只需配置model扫描目录 和dsmName
public void configPlugin(PluginLoader pluginLoader) {
  //第一个数据库
  ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(new DruidDataSourceProvider("default"), true);
  activeRecordPlugin.addIncludePaths("cn.dreampie.resource");
  pluginLoader.add(activeRecordPlugin);
}

1.极简的route设计,完全融入普通方法的方式,方法参数就是请求参数,方法返回值就是数据返回值

  @GET("/users/:name")
  //在路径中自定义解析的参数 如果有其他符合 也可以用 /users/{name}
  // 参数名就是方法变量名  除路径参数之外的参数也可以放在方法参数里  传递方式 user={json字符串}
  public Map find(String name,User user) {
    // return Lister.of(name);
    return Maper.of("k1", "v1,name:" + name, "k2", "v2");
    //返回什么数据直接return
  }

2.极简的activerecord设计,数据操作只需短短的一行,支持批量保存对象

  //批量保存
  User u1 = new User().set("username", "test").set("providername", "test").set("password", "123456");
  User u2 = new User().set("username", "test").set("providername", "test").set("password", "123456");
  User.dao.save(u1,u2);

  //普通保存
  User u = new User().set("username", "test").set("providername", "test").set("password", "123456");
  u.save();

  //更新
  u.update();
  //条件更新
  User.dao.updateBy(columns,where,paras);
  User.dao.updateAll(columns,paras);

  //删除
  u.deleted();
  //条件删除
  User.dao.deleteBy(where,paras);
  User.dao.deleteAll();

  //查询
  User.dao.findById(id);
  User.dao.findBy(where,paras);
  User.dao.findAll();

  //分页
  User.dao.paginateBy(pageNumber,pageSize,where,paras);
  User.dao.paginateAll(pageNumber,pageSize);

3.极简的客户端设计,支持各种请求,文件上传和文件下载(支持断点续传)

  Client httpClient=null;//创建客户端对象
  //启动resty-example项目,即可测试客户端
  String apiUrl = "http://localhost:8081/api/v1.0";
  //如果不需要 使用账号登陆
  //httpClient = new Client(apiUrl);
  //如果有账号权限限制  需要登陆
  httpClient = new Client(apiUrl, "/tests/login", "u", "123");

  //该请求必须  登陆之后才能访问  未登录时返回 401  未认证
  ClientRequest authRequest = new ClientRequest("/users");
  ClientResult authResult = httpClient.build(authRequest).get();
  System.out.println(authResult.getResult());

  //get
  ClientRequest getRequest = new ClientRequest("/tests");
  ClientResult getResult = httpClient.build(getRequest).get();
  System.out.println(getResult.getResult());

  //post
  ClientRequest postRequest = new ClientRequest("/tests");
  postRequest.addParam("test", Jsoner.toJSONString(Maper.of("a", "谔谔")));
  ClientResult postResult = httpClient.build(postRequest).post();
  System.out.println(postResult.getResult());

  //put
  ClientRequest putRequest = new ClientRequest("/tests/x");
  ClientResult putResult = httpClient.build(putRequest).put();
  System.out.println(putResult.getResult());


  //delete
  ClientRequest deleteRequest = new ClientRequest("/tests/a");
  ClientResult deleteResult = httpClient.build(deleteRequest).delete();
  System.out.println(deleteResult.getResult());


  //upload
  ClientRequest uploadRequest = new ClientRequest("/tests/resty");
  uploadRequest.addUploadFiles("resty", ClientTest.class.getResource("/resty.jar").getFile());
  uploadRequest.addParam("des", "test file  paras  测试笔");
  ClientResult uploadResult = httpClient.build(uploadRequest).post();
  System.out.println(uploadResult.getResult());


  //download  支持断点续传
  ClientRequest downloadRequest = new ClientRequest("/tests/file");
  downloadRequest.setDownloadFile(ClientTest.class.getResource("/resty.jar").getFile().replace(".jar", "x.jar"));
  ClientResult downloadResult = httpClient.build(downloadRequest).get();
  System.out.println(downloadResult.getResult());

4.支持多数据源和嵌套事务(使用场景:需要访问多个数据库的应用,或者作为公司内部的数据中间件向客户端提供数据访问api等)

  // 在resource里使用事务,也就是controller里,rest的世界认为所以的请求都表示资源,所以这儿叫resource
  @GET("/users")
  @Transaction(name = {"default", "demo"}) //多数据源的事务,如果你只有一个数据库  直接@Transaction 不需要参数
  public User transaction() {
  //TODO 用model执行数据库的操作  只要有操作抛出异常  两个数据源 都会回滚  虽然不是分布式事务  也能保证代码块的数据执行安全
  }

  // 如果你需要在service里实现事务,通过java动态代理(必须使用接口,jdk设计就是这样)
  public interface UserService {
    @Transaction(name = {"demo"})//service里添加多数据源的事务,如果你只有一个数据库  直接@Transaction 不需要参数
    public User save(User u);
  }
  // 在resource里使用service层的 事务
  // @Transaction(name = {"demo"})的注解需要写在service的接口上
  // 注意java的自动代理必须存在接口
  // TransactionAspect 是事务切面 ,你也可以实现自己的切面比如日志的Aspect,实现Aspect接口
  // 再private UserService userService = AspectFactory.newInstance(new UserServiceImpl(), new TransactionAspect(),new LogAspect());
  private UserService userService = AspectFactory.newInstance(new UserServiceImpl(), new TransactionAspect());

5.极简的权限设计,可以通过cache支持分布式session,你只需要实现一个简单接口和添加一个拦截器,即可实现基于url的权限设计

  public void configInterceptor(InterceptorLoader interceptorLoader) {
    //权限拦截器 放在第一位 第一时间判断 避免执行不必要的代码
    interceptorLoader.add(new SecurityInterceptor(new MyAuthenticateService()));
  }

  //实现接口
  public class MyAuthenticateService implements AuthenticateService {
    //登陆时 通过name获取用户的密码和权限信息
    public Principal findByName(String name) {
      DefaultPasswordService defaultPasswordService = new DefaultPasswordService();

      Principal principal = new Principal(name, defaultPasswordService.hash("123"), new HashSet<String>() {{
        add("api");
      }});
      return principal;
    }
    //基础的权限总表  所以的url权限都放在这儿  你可以通过 文件或者数据库或者直接代码 来设置所有权限
    public Set<Credential> loadAllCredentials() {
      Set<Credential> credentials = new HashSet<Credential>();
      credentials.add(new Credential("GET", "/api/v1.0/users**", "users"));
      return credentials;
    }
  }

6.极简的缓存设计,可扩展,非常简单即可启用model的自动缓存功能

  //启用缓存并在要自动使用缓存的model上
  //config application.properties  app.cacheEnabled=true
  //开启缓存@Table(name = "sec_user", cached = true)

  @Table(name = "sec_user", cached = true)
  public class User extends Model<User> {
    public static User dao = new User();

  }

7.下载文件,只需要直接return file

  @GET("/files")
  public File file() {
    return new File(path);
  }

8.上传文件,注解配置把文件写到服务器

  @POST("/files")
  @FILE(dir = "/upload/") //配置上传文件的相关信息
  public UploadedFile file(UploadedFile file) {
    return file;
  }

9.当然也是支持传统的web开发,你可以自己实现数据解析,在config里添加自定义的解析模板

  public void configConstant(ConstantLoader constantLoader) {
    // 通过后缀来返回不同的数据类型  你可以自定义自己的  render  如:FreemarkerRender
    //默认已添加json和text的支持,只需要把自定义的Render add即可
    // constantLoader.addRender("json", new JsonRender());
  }

二、运行example示例:

1.在本地mysql数据库里创建demo,example数据库,对应application.properties的数据库配置

2.运行resty-example下的pom.xml->flyway-maven-plugin:migrate,自动根具resources下db目录下的数据库文件生成数据库表结构

3.运行resty-example下的pom.xml->tomcat6-maven-plugin:run,启动example程序

提醒:推荐idea作为开发ide,使用分模块的多module开发

License Apache License V2

捐赠: 支付宝

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: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) 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 (d) 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 [yyyy] [name of copyright owner] 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.

简介

sample restful framework 展开 收起
Java 等 2 种语言
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

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

搜索帮助

14c37bed 8189591 565d56ea 8189591