這篇文章主要講解了“Restful框架有哪些優(yōu)點(diǎn)”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Restful框架有哪些優(yōu)點(diǎn)”吧!
10年的雞西梨樹(shù)網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營(yíng)銷(xiāo)推廣的優(yōu)勢(shì)是能夠根據(jù)用戶(hù)設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整雞西梨樹(shù)建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)公司從事“雞西梨樹(shù)網(wǎng)站設(shè)計(jì)”,“雞西梨樹(shù)網(wǎng)站推廣”以來(lái),每個(gè)客戶(hù)項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
擁有jfinal/activejdbc一樣的activerecord的簡(jiǎn)潔設(shè)計(jì),使用更簡(jiǎn)單的restful框架。
restful的api設(shè)計(jì),是作為restful的服務(wù)端***選擇(使用場(chǎng)景:客戶(hù)端和服務(wù)端解藕,用于對(duì)靜態(tài)的html客戶(hù)端(mvvm等),ios,andriod等提供服務(wù)端的api接口)
一、獨(dú)有優(yōu)點(diǎn):
1.極簡(jiǎn)的route設(shè)計(jì):
@GET("/users/:name") //在路徑中自定義解析的參數(shù) 如果有其他符合 也可以用 /users/{name} // 參數(shù)名就是方法變量名 除路徑參數(shù)之外的參數(shù)也可以放在方法參數(shù)里 傳遞方式 user={json字符串} public Map find(String name,User user) { // return Lister.of(name); return Maper.of("k1", "v1,name:" + name, "k2", "v2");//返回什么數(shù)據(jù)直接return,完全融入普通方法的方式 }
2.極簡(jiǎn)的activerecord設(shè)計(jì),數(shù)據(jù)操作只需短短的一行 ,支持批量保存對(duì)象
//批量保存 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(where,paras); User.dao.updateAll(columns,where,paras); //刪除 u.deleted(); //條件刪除 User.dao.deleteBy(where,paras); User.dao.deleteAll(); //查詢(xún) User.dao.findById(id); User.dao.findBy(where,paras); User.dao.findAll(); //分頁(yè) User.dao.paginateBy(pageNumber,pageSize,where,paras); User.dao.paginateAll(pageNumber,pageSize);
3.極簡(jiǎn)的客戶(hù)端設(shè)計(jì),支持各種請(qǐng)求,文件上傳和文件下載(支持?jǐn)帱c(diǎn)續(xù)傳)
Client client=null;//創(chuàng)建客戶(hù)端對(duì)象 //啟動(dòng)resty-example項(xiàng)目,即可測(cè)試客戶(hù)端 String apiUrl = "http://localhost:8081/api/v1.0"; //如果不需要 使用賬號(hào)登陸 //client = new Client(apiUrl); //如果有賬號(hào)權(quán)限限制 需要登陸 client = new Client(apiUrl, "/tests/login", "u", "123"); //該請(qǐng)求必須 登陸之后才能訪(fǎng)問(wèn) 未登錄時(shí)返回 401 未認(rèn)證 ClientRequest authRequest = new ClientRequest("/users", HttpMethod.GET); ResponseData authResult = client.build(authRequest).ask(); System.out.println(authResult.getData()); //get ClientRequest getRequest = new ClientRequest("/tests", HttpMethod.GET); ResponseData getResult = client.build(getRequest).ask(); System.out.println(getResult.getData()); //post ClientRequest postRequest = new ClientRequest("/tests", HttpMethod.POST); postRequest.addParameter("test", Jsoner.toJSONString(Maper.of("a", "諤諤"))); ResponseData postResult = client.build(postRequest).ask(); System.out.println(postResult.getData()); //put ClientRequest putRequest = new ClientRequest("/tests/x", HttpMethod.PUT); ResponseData putResult = client.build(putRequest).ask(); System.out.println(putResult.getData()); //delete ClientRequest deleteRequest = new ClientRequest("/tests/a", HttpMethod.DELETE); ResponseData deleteResult = client.build(deleteRequest).ask(); System.out.println(deleteResult.getData()); //upload ClientRequest uploadRequest = new ClientRequest("/tests/resty", HttpMethod.POST); uploadRequest.addUploadFiles("resty", ClientTest.class.getResource("/resty.jar").getFile()); uploadRequest.addParameter("des", "test file paras 測(cè)試筆"); ResponseData uploadResult = client.build(uploadRequest).ask(); System.out.println(uploadResult.getData()); //download 支持?jǐn)帱c(diǎn)續(xù)傳 ClientRequest downloadRequest = new ClientRequest("/tests/file", HttpMethod.GET); downloadRequest.setDownloadFile(ClientTest.class.getResource("/resty.jar").getFile().replace(".jar", "x.jar")); ResponseData downloadResult = client.build(downloadRequest).ask(); System.out.println(downloadResult.getData());
4.支持多數(shù)據(jù)源和嵌套事務(wù)(使用場(chǎng)景:需要訪(fǎng)問(wèn)多個(gè)數(shù)據(jù)庫(kù)的應(yīng)用,或者作為公司內(nèi)部的數(shù)據(jù)中間件向客戶(hù)端提供數(shù)據(jù)訪(fǎng)問(wèn)api等)5.極簡(jiǎn)的權(quán)限設(shè)計(jì),你只需要實(shí)現(xiàn)一個(gè)簡(jiǎn)單接口和添加一個(gè)攔截器,即可實(shí)現(xiàn)基于url的權(quán)限設(shè)計(jì)
// 在resource里使用事務(wù),也就是controller里,rest的世界認(rèn)為所以的請(qǐng)求都表示資源,所以這兒叫resource @GET("/users") @Transaction(name = {DS.DEFAULT_DS_NAME, "demo"}) //多數(shù)據(jù)源的事務(wù),如果你只有一個(gè)數(shù)據(jù)庫(kù) 直接@Transaction 不需要參數(shù) public User transaction() { //TODO 用model執(zhí)行數(shù)據(jù)庫(kù)的操作 只要有操作拋出異常 兩個(gè)數(shù)據(jù)源 都會(huì)回滾 雖然不是分布式事務(wù) 也能保證代碼塊的數(shù)據(jù)執(zhí)行安全 } // 如果你需要在service里實(shí)現(xiàn)事務(wù),通過(guò)java動(dòng)態(tài)代理(必須使用接口,jdk設(shè)計(jì)就是這樣) public interface UserService { @Transaction(name = {DS.DEFAULT_DS_NAME, "demo"})//service里添加多數(shù)據(jù)源的事務(wù),如果你只有一個(gè)數(shù)據(jù)庫(kù) 直接@Transaction 不需要參數(shù) public User save(User u); } // 在resource里使用service層的 事務(wù) // @Transaction(name = {DS.DEFAULT_DS_NAME, "demo"})的注解需要寫(xiě)在service的接口上 // 注意java的自動(dòng)代理必須存在接口 // TransactionAspect 是事務(wù)切面 ,你也可以實(shí)現(xiàn)自己的切面比如日志的Aspect,實(shí)現(xiàn)Aspect接口 // 再private UserService userService = AspectFactory.newInstance(new UserServiceImpl(), new TransactionAspect(),new LogAspect()); private UserService userService = AspectFactory.newInstance(new UserServiceImpl(), new TransactionAspect());
5.極簡(jiǎn)的權(quán)限設(shè)計(jì),你只需要實(shí)現(xiàn)一個(gè)簡(jiǎn)單接口和添加一個(gè)攔截器,即可實(shí)現(xiàn)基于url的權(quán)限設(shè)計(jì)
public void configInterceptor(InterceptorLoader interceptorLoader) { //權(quán)限攔截器 放在***位 ***時(shí)間判斷 避免執(zhí)行不必要的代碼 interceptorLoader.add(new SecurityInterceptor(new MyAuthenticateService())); } //實(shí)現(xiàn)接口 public class MyAuthenticateService implements AuthenticateService { //登陸時(shí) 通過(guò)name獲取用戶(hù)的密碼和權(quán)限信息 public Principal findByName(String name) { DefaultPasswordService defaultPasswordService = new DefaultPasswordService(); Principal principal = new Principal(name, defaultPasswordService.hash("123"), new HashSet<String>() {{ add("api"); }}); return principal; } //基礎(chǔ)的權(quán)限總表 所以的url權(quán)限都放在這兒 你可以通過(guò) 文件或者數(shù)據(jù)庫(kù)或者直接代碼 來(lái)設(shè)置所有權(quán)限 public Set<Permission> loadAllPermissions() { Set<Permission> permissions = new HashSet<Permission>(); permissions.add(new Permission("GET", "/api/transactions**", "api")); return permissions; } }
6.極簡(jiǎn)的緩存設(shè)計(jì),可擴(kuò)展,非常簡(jiǎn)單即可啟用model的自動(dòng)緩存功能7.下載文件,只需要直接return file
public void configConstant(ConstantLoader constantLoader) { //啟用緩存并在要自動(dòng)使用緩存的model上 開(kāi)啟緩存@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(); }
7.下載文件,只需要直接return file
@GET("/files") public File file() { return new File(path); }
8.上傳文件,通過(guò)getFiles,getFile把文件寫(xiě)到服務(wù)器
@POST("/files") public UploadedFile file() { //Hashtable<String, UploadedFile> uploadedFiles=getFiles(); return getFile(name); }
9.當(dāng)然也是支持傳統(tǒng)的web開(kāi)發(fā),你可以自己實(shí)現(xiàn)數(shù)據(jù)解析,在config里添加自定義的解析模板
public void configConstant(ConstantLoader constantLoader) { // 通過(guò)后綴來(lái)返回不同的數(shù)據(jù)類(lèi)型 你可以自定義自己的 render 如:FreemarkerRender // constantLoader.addRender("json", new JsonRender()); //默認(rèn)已添加json和text的支持,只需要把自定義的Render add即可 }
二、運(yùn)行example示例:
1.運(yùn)行根目錄下的pom.xml->install (把相關(guān)的插件安裝到本地,功能完善之后發(fā)布到maven就不需要這樣了)
2.在本地MySQL數(shù)據(jù)庫(kù)里創(chuàng)建demo,example數(shù)據(jù)庫(kù),對(duì)應(yīng)application.properties的數(shù)據(jù)庫(kù)配置
3.運(yùn)行resty-example下的pom.xml->flyway-maven-plugin:migration,自動(dòng)根具resources下db目錄下的數(shù)據(jù)庫(kù)文件生成數(shù)據(jù)庫(kù)表結(jié)構(gòu)
4.運(yùn)行resty-example下的pom.xml->tomcat7-maven-plugin:run,啟動(dòng)example程序
提醒:推薦idea作為開(kāi)發(fā)ide,使用分模塊的多module開(kāi)發(fā)。
感謝各位的閱讀,以上就是“Restful框架有哪些優(yōu)點(diǎn)”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)Restful框架有哪些優(yōu)點(diǎn)這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
分享名稱(chēng):Restful框架有哪些優(yōu)點(diǎn)
本文地址:http://chinadenli.net/article0/poojoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、小程序開(kāi)發(fā)、動(dòng)態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈、定制網(wǎng)站、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)