自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

極簡的Restful框架推薦推薦:Resty(服務(wù)端+客戶端)

開發(fā) 前端
resty一款極簡的restful輕量級的web框架,擁有jfinal/activejdbc一樣的activerecord的簡潔設(shè)計,使用更簡單的restful框架。

擁有jfinal/activejdbc一樣的activerecord的簡潔設(shè)計,使用更簡單的restful框架。

restful的api設(shè)計,是作為restful的服務(wù)端***選擇(使用場景:客戶端和服務(wù)端解藕,用于對靜態(tài)的html客戶端(mvvm等),ios,andriod等提供服務(wù)端的api接口)

一、獨有優(yōu)點:

1.極簡的route設(shè)計:

  1.   @GET("/users/:name")  //在路徑中自定義解析的參數(shù) 如果有其他符合 也可以用 /users/{name}  
  2. // 參數(shù)名就是方法變量名  除路徑參數(shù)之外的參數(shù)也可以放在方法參數(shù)里  傳遞方式 user={json字符串}  
  3. public Map find(String name,User user) {  
  4.   // return Lister.of(name);  
  5.   return Maper.of("k1""v1,name:" + name, "k2""v2");//返回什么數(shù)據(jù)直接return,完全融入普通方法的方式  

2.極簡的activerecord設(shè)計,數(shù)據(jù)操作只需短短的一行 ,支持批量保存對象

 

 

  1. //批量保存  
  2. User u1 = new User().set("username""test").set("providername""test").set("password""123456");  
  3. User u2 = new User().set("username""test").set("providername""test").set("password""123456");  
  4. User.dao.save(u1,u2);  
  5.    
  6. //普通保存  
  7. User u = new User().set("username""test").set("providername""test").set("password""123456");  
  8. u.save();  
  9.    
  10. //更新  
  11. u.update();  
  12. //條件更新  
  13. User.dao.updateBy(where,paras);  
  14. User.dao.updateAll(columns,where,paras);  
  15.    
  16. //刪除  
  17. u.deleted();  
  18. //條件刪除  
  19. User.dao.deleteBy(where,paras);  
  20. User.dao.deleteAll();  
  21.    
  22. //查詢  
  23. User.dao.findById(id);  
  24. User.dao.findBy(where,paras);  
  25. User.dao.findAll();  
  26.    
  27. //分頁  
  28. User.dao.paginateBy(pageNumber,pageSize,where,paras);  
  29. User.dao.paginateAll(pageNumber,pageSize); 

3.極簡的客戶端設(shè)計,支持各種請求,文件上傳和文件下載(支持?jǐn)帱c續(xù)傳)

 

  1. Client client=null;//創(chuàng)建客戶端對象  
  2. //啟動resty-example項目,即可測試客戶端  
  3. String apiUrl = "http://localhost:8081/api/v1.0";  
  4. //如果不需要 使用賬號登陸  
  5. //client = new Client(apiUrl);  
  6. //如果有賬號權(quán)限限制  需要登陸  
  7. client = new Client(apiUrl, "/tests/login""u""123");  
  8.    
  9. //該請求必須  登陸之后才能訪問  未登錄時返回 401  未認(rèn)證  
  10. ClientRequest authRequest = new ClientRequest("/users", HttpMethod.GET);  
  11. ResponseData authResult = client.build(authRequest).ask();  
  12. System.out.println(authResult.getData());  
  13.    
  14. //get  
  15. ClientRequest getRequest = new ClientRequest("/tests", HttpMethod.GET);  
  16. ResponseData getResult = client.build(getRequest).ask();  
  17. System.out.println(getResult.getData());  
  18.    
  19. //post  
  20. ClientRequest postRequest = new ClientRequest("/tests", HttpMethod.POST);  
  21. postRequest.addParameter("test", Jsoner.toJSONString(Maper.of("a""諤諤")));  
  22. ResponseData postResult = client.build(postRequest).ask();  
  23. System.out.println(postResult.getData());  
  24.    
  25. //put  
  26. ClientRequest putRequest = new ClientRequest("/tests/x", HttpMethod.PUT);  
  27. ResponseData putResult = client.build(putRequest).ask();  
  28. System.out.println(putResult.getData());  
  29.    
  30.    
  31. //delete  
  32. ClientRequest deleteRequest = new ClientRequest("/tests/a", HttpMethod.DELETE);  
  33. ResponseData deleteResult = client.build(deleteRequest).ask();  
  34. System.out.println(deleteResult.getData());  
  35.    
  36.    
  37. //upload  
  38. ClientRequest uploadRequest = new ClientRequest("/tests/resty", HttpMethod.POST);  
  39. uploadRequest.addUploadFiles("resty", ClientTest.class.getResource("/resty.jar").getFile());  
  40. uploadRequest.addParameter("des""test file  paras  測試筆");  
  41. ResponseData uploadResult = client.build(uploadRequest).ask();  
  42. System.out.println(uploadResult.getData());  
  43.    
  44.    
  45. //download  支持?jǐn)帱c續(xù)傳  
  46. ClientRequest downloadRequest = new ClientRequest("/tests/file", HttpMethod.GET);  
  47. downloadRequest.setDownloadFile(ClientTest.class.getResource("/resty.jar").getFile().replace(".jar""x.jar"));  
  48. ResponseData downloadResult = client.build(downloadRequest).ask();  
  49. System.out.println(downloadResult.getData()); 

4.支持多數(shù)據(jù)源和嵌套事務(wù)(使用場景:需要訪問多個數(shù)據(jù)庫的應(yīng)用,或者作為公司內(nèi)部的數(shù)據(jù)中間件向客戶端提供數(shù)據(jù)訪問api等)5.極簡的權(quán)限設(shè)計,你只需要實現(xiàn)一個簡單接口和添加一個攔截器,即可實現(xiàn)基于url的權(quán)限設(shè)計

  1. // 在resource里使用事務(wù),也就是controller里,rest的世界認(rèn)為所以的請求都表示資源,所以這兒叫resource  
  2. @GET("/users")  
  3. @Transaction(name = {DS.DEFAULT_DS_NAME, "demo"}) //多數(shù)據(jù)源的事務(wù),如果你只有一個數(shù)據(jù)庫  直接@Transaction 不需要參數(shù)  
  4. public User transaction() {  
  5. //TODO 用model執(zhí)行數(shù)據(jù)庫的操作  只要有操作拋出異常  兩個數(shù)據(jù)源 都會回滾  雖然不是分布式事務(wù)  也能保證代碼塊的數(shù)據(jù)執(zhí)行安全  
  6. }  
  7.    
  8. // 如果你需要在service里實現(xiàn)事務(wù),通過java動態(tài)代理(必須使用接口,jdk設(shè)計就是這樣)  
  9. public interface UserService {  
  10.   @Transaction(name = {DS.DEFAULT_DS_NAME, "demo"})//service里添加多數(shù)據(jù)源的事務(wù),如果你只有一個數(shù)據(jù)庫  直接@Transaction 不需要參數(shù)  
  11.   public User save(User u);  
  12. }  
  13. // 在resource里使用service層的 事務(wù)  
  14. // @Transaction(name = {DS.DEFAULT_DS_NAME, "demo"})的注解需要寫在service的接口上  
  15. // 注意java的自動代理必須存在接口  
  16. // TransactionAspect 是事務(wù)切面 ,你也可以實現(xiàn)自己的切面比如日志的Aspect,實現(xiàn)Aspect接口  
  17. // 再private UserService userService = AspectFactory.newInstance(new UserServiceImpl(), new TransactionAspect(),new LogAspect());  
  18. private UserService userService = AspectFactory.newInstance(new UserServiceImpl(), new TransactionAspect()); 

5.極簡的權(quán)限設(shè)計,你只需要實現(xiàn)一個簡單接口和添加一個攔截器,即可實現(xiàn)基于url的權(quán)限設(shè)計

  1. public void configInterceptor(InterceptorLoader interceptorLoader) {  //權(quán)限攔截器 放在***位 ***時間判斷 避免執(zhí)行不必要的代碼  
  2.   interceptorLoader.add(new SecurityInterceptor(new MyAuthenticateService()));  
  3. }  
  4. //實現(xiàn)接口  
  5. public class MyAuthenticateService implements AuthenticateService {    
  6. //登陸時 通過name獲取用戶的密碼和權(quán)限信息  
  7.   public Principal findByName(String name) {  
  8.     DefaultPasswordService defaultPasswordService = new DefaultPasswordService();  
  9.    
  10.     Principal principal = new Principal(name, defaultPasswordService.hash("123"), new HashSet<String>() {{  
  11.       add("api");  
  12.     }});      
  13.     return principal;  
  14.   }    
  15.   //基礎(chǔ)的權(quán)限總表  所以的url權(quán)限都放在這兒  你可以通過 文件或者數(shù)據(jù)庫或者直接代碼 來設(shè)置所有權(quán)限  
  16.   public Set<Permission> loadAllPermissions() {  
  17.     Set<Permission> permissions = new HashSet<Permission>();  
  18.     permissions.add(new Permission("GET""/api/transactions**""api"));      
  19.     return permissions;  
  20.   }  

6.極簡的緩存設(shè)計,可擴(kuò)展,非常簡單即可啟用model的自動緩存功能7.下載文件,只需要直接return file

  1. public void configConstant(ConstantLoader constantLoader) {  
  2.   //啟用緩存并在要自動使用緩存的model上  開啟緩存@Table(name = "sec_user", cached = true)  
  3.   constantLoader.setCacheEnable(true);  
  4. }  
  5.    
  6. @Table(name = "sec_user", cached = true)  
  7. public class User extends Model<User> {  
  8.   public static User dao = new User();  
  9.    

 

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

  1. @GET("/files")  
  2. public File file() {  
  3.   return new File(path);  

8.上傳文件,通過getFiles,getFile把文件寫到服務(wù)器

  1. @POST("/files")  
  2. public UploadedFile file() {  
  3.   //Hashtable<String, UploadedFile> uploadedFiles=getFiles();  
  4.   return getFile(name);  

9.當(dāng)然也是支持傳統(tǒng)的web開發(fā),你可以自己實現(xiàn)數(shù)據(jù)解析,在config里添加自定義的解析模板

  1. public void configConstant(ConstantLoader constantLoader) {  
  2.   // 通過后綴來返回不同的數(shù)據(jù)類型  你可以自定義自己的  render  如:FreemarkerRender  
  3.   // constantLoader.addRender("json", new JsonRender());  
  4.   //默認(rèn)已添加json和text的支持,只需要把自定義的Render add即可  

二、運行example示例:

1.運行根目錄下的pom.xml->install (把相關(guān)的插件安裝到本地,功能完善之后發(fā)布到maven就不需要這樣了)

2.在本地mysql數(shù)據(jù)庫里創(chuàng)建demo,example數(shù)據(jù)庫,對應(yīng)application.properties的數(shù)據(jù)庫配置

3.運行resty-example下的pom.xml->flyway-maven-plugin:migration,自動根具resources下db目錄下的數(shù)據(jù)庫文件生成數(shù)據(jù)庫表結(jié)構(gòu)

4.運行resty-example下的pom.xml->tomcat7-maven-plugin:run,啟動example程序

提醒:推薦idea作為開發(fā)ide,使用分模塊的多module開發(fā)。

源碼鏈接:OSC -> Resty   Github -> Resty

本文出自:http://my.oschina.net/wangrenhui1990/blog/365310

責(zé)任編輯:林師授 來源: 王仁輝的博客
相關(guān)推薦

2021-10-18 05:00:38

語言GoRequestHTTP

2009-08-21 15:59:22

服務(wù)端與客戶端通信

2021-05-25 08:20:37

編程技能開發(fā)

2011-09-09 09:44:23

WCF

2009-08-21 16:14:52

服務(wù)端與客戶端通信

2021-08-01 23:18:21

Redis Golang命令

2024-03-06 14:58:52

客戶端微服務(wù)架構(gòu)

2010-11-19 14:22:04

oracle服務(wù)端

2014-12-22 11:04:30

Windows AzuiPhone虛擬機(jī)

2010-03-18 17:47:07

Java 多客戶端通信

2023-03-06 08:01:56

MySQLCtrl + C

2009-08-21 15:36:41

服務(wù)端與客戶端

2009-08-21 15:54:40

服務(wù)端與客戶端

2019-07-05 17:00:33

Redis數(shù)據(jù)庫可視化管理

2015-08-26 17:31:03

UbuntuBT客戶端

2011-05-18 09:56:31

UbuntuLinux客戶端

2023-04-03 08:13:05

MySQLCtrl + C

2012-11-28 11:05:42

IBMdW

2022-08-09 07:32:03

MySQLWindowsLinux

2010-06-01 13:19:04

點贊
收藏

51CTO技術(shù)棧公眾號