極簡的restful輕量級的web框架:Resty
擁有 jfinal / activejdbc 一樣的 activerecord 的簡潔設計,使用更簡單的 restful 框架。
restful 的 api 設計,是作為 restful 的服務端最佳選擇(使用場景:客戶端和服務端解藕,用于對靜態的 html 客戶端(mvvm等),ios,andriod 等提供服務端的api接口)
獨有優點:
-
極簡的 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,完全融入普通方法的方式 }
-
支持多數據源和嵌套事務(使用場景:需要訪問多個數據庫的應用,或者作為公司內部的數據中間件向客戶端提供數據訪問api等)
// 在resource里使用事務,也就是controller里,rest的世界認為所以的請求都表示資源,所以這兒叫resource @GET("/users") @Transaction(name = {DS.DEFAULT_DS_NAME, "demo"}) //多數據源的事務,如果你只有一個數據庫 直接@Transaction 不需要參數 public User transaction() { //TODO 用model執行數據庫的操作 只要有操作拋出異常 兩個數據源 都會回滾 雖然不是分布式事務 也能保證代碼塊的數據執行安全 } // 如果你需要在service里實現事務,通過java動態代理(必須使用接口,jdk設計就是這樣) public interface UserService { @Transaction(name = {DS.DEFAULT_DS_NAME, "demo"})//service里添加多數據源的事務,如果你只有一個數據庫 直接@Transaction 不需要參數 public User save(User u); } // 在resource里使用service層的 事務 // @Transaction(name = {DS.DEFAULT_DS_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());
-
極簡的權限設計,你只需要實現一個簡單接口和添加一個攔截器,即可實現基于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<Permission> loadAllPermissions() { Set<Permission> permissions = new HashSet<Permission>(); permissions.add(new Permission("GET", "/api/transactions**", "api")); return permissions; } }
-
極簡的緩存設計,可擴展,非常簡單即可啟用model的自動緩存功能
public void configConstant(ConstantLoader constantLoader) { //啟用緩存并在要自動使用緩存的model上 開啟緩存@Table(name = "sec_user", cached = true) constantLoader.setCacheEnable(true); } @Table(name = "sec_user", cached = true) public class User extends Model<User> { public static User dao = new User(); }
-
下載文件,只需要直接return file
@GET("/files") public File file() { return new File(path); }
-
上傳文件,通過getFiles,getFile把文件寫到服務器
@POST("/files") public UploadedFile file() { //Hashtable<String, UploadedFile> uploadedFiles=getFiles(); return getFile(name); }
-
當然也是支持傳統的web開發,你可以自己實現數據解析,在config里添加自定義的解析模板
public void configConstant(ConstantLoader constantLoader) { // 通過后綴來返回不同的數據類型 你可以自定義自己的 render 如:FreemarkerRender // constantLoader.addRender("json", new JsonRender());//默認已添加json和text的支持,只需要把自定義的Render add即可 }
運行example示例:
-
運行根目錄下的pom.xml->install (把相關的插件安裝到本地,穩定版之后發布到maven就不需要這樣了)
-
在本地mysql數據庫里創建demo,example數據庫,對應application.properties的數據庫配置
-
運行resty-example下的pom.xml->flyway-maven-plugin:migration,生成resources下得數據庫表創建文件
-
運行resty-example下的pom.xml->tomcat7-maven-plugin:run,啟動example程序
注意:推薦idea作為開發ide,使用分模塊的多module開發
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!