SpringBoot20-REST API介面
一、REST 簡介
1.介面的意義:
系統關聯基於介面來實現,介面測試將複雜的系統關聯簡化
介面功能比較單一,能更好的進行[測試覆蓋],容易實現[自動化持續整合]
單元測試之後,UI測試之前,介面測試比單元測試粒度粗
2.Web Service:
一種跨程式語言和操作平臺的遠端呼叫技術
實現方式:SOAP和REST
3.SOAP/">SOAP:
簡單物件訪問協議(Simple Object Acsess Protocol)
資料交換的一種協議,輕量級、簡單的、基於Xml的協議
4.REST:
表示性狀態轉換(Representational State Transfer)
一種系統(軟體)架構風格(非標準),一種分散式系統的應用層解決方案
安全性 SOAP>REST、效率和易用性 REST>SOAP、成熟度 SOAP>REST
5.RESTFUL:
RESTFUL 是REST的形容詞
RESTFUL API 是指REST風格的介面
RESTFUL與REST、rest、resetful、RESTful意義上等同
二、RESTFUL介面
1.優勢與特點
實現Client和Server端解耦,可降低開發的複雜性,提高系統的可伸縮性 資源為核心思想(面向資源的CRUD):HTTP[傳輸協議]升級為[應用傳輸協議] 創造資源C:POST 獲取資源R:GET僅獲取資源頭資訊:HEAD 更新資源U:PUT更新資源部分屬性:PATCH (使用較少,一般用POST代替) 刪除資源D:DELETE 冪等性:傳送一次和多次請求引起的[邊界效應]一致 安全性:僅獲取書籍,不具有[邊界效應]GET、HEAD、OPTIONS
2.REST介面風格:
協議:使用https協議 域名:https://api.toly1994.com 版本控制:將版本號放在URL或Head二中 路徑:只能包含名詞,不能用動詞 過濾資訊:?limit=10?offset=10?page=1?sortby=name Hypermedia API :在返回結果中提供其他連線資源,連向其他API 驗證:確定身份 授權:許可權設定 通用返回結果:如: { "msg":"uri not found", "code":0001, "data":null, "request":"GET\/v2\/photo\/132" }

常見的Http狀態碼.png
三、程式碼測試:基於 ofollow,noindex">SpringBoot-07-之資料庫JPA(CRUD) 修改
測試使用的RESTFUL介面一覽:
http://localhost:8080/api/sword: GET 獲取所用劍的資訊 http://localhost:8080/api/sword: POST 新建一把劍 http://localhost:8080/api/sword/26: GET 獲取一把指定id的劍資訊 http://localhost:8080/api/sword/40: PUT 修改一把指定id的劍資訊 http://localhost:8080/api/sword/39: DELETE 刪除指定id的劍 http://localhost:8080/api/sword/21/name: GET 查詢指定id的劍的名稱
1.GET 獲取所用劍的資訊 http://localhost:8080/api/sword
/** * 查詢所有:GET http://localhost:8080/api/sword * * @return 查詢所有 */ @GetMapping(value = "/sword") public ResultBean findAllToJson() { return ResultHandler.ok(mSwordRepository.findAll()); }

列出所有.png
2.POST 新建一把劍 http://localhost:8080/api/sword
/** * 新建一把劍:POST GET http://localhost:8080/api/sword * @param sword 劍 * @return 劍 */ @PostMapping(value = "/sword") public ResultBean addOne(@ModelAttribute Sword sword) { Sword save = mSwordRepository.save(sword); return ResultHandler.ok(save); }

新增一個.png
3.GET 獲取一把指定id的劍資訊 http://localhost:8080/api/sword/26
/** * 根據id查詢 GET http://localhost:8080/api/sword/26 * * @param id id * @return 劍 */ @GetMapping(value = "/swords/find/{id}") public ResultBean find(@PathVariable("id") Integer id) { return ResultHandler.ok(mSwordRepository.findById(id).get()); }

查詢一個.png
4.PUT 修改一把指定id的劍資訊 http://localhost:8080/api/sword/26
/** * 根據id更新PUT http://localhost:8080/api/sword/40 * * @param id id * @return 劍 */ @PutMapping(value = "/sword/{id}") public ResultBean update(@PathVariable("id") Integer id, @ModelAttribute Sword sword) { return ResultHandler.ok(mSwordRepository.save(sword)); }

更新一個.png
5.DELETE 刪除指定id的劍 http://localhost:8080/api/sword/39
/** * 根據id刪除 DELETE http://localhost:8080/api/sword/39 * * @param id id */ @DeleteMapping(value = "/sword/{id}") public ResultBean insert(@PathVariable("id") Integer id) { ResultBean resultBean = find(id); mSwordRepository.deleteById(id); return resultBean; }

刪除一個.png
6.GET根據id查詢劍的名稱: http://localhost:8080/api/sword/21/name
/** * 根據id查詢劍的名稱 GET http://localhost:8080/api/sword/21/name * * @param id id * @return 劍 */ @GetMapping(value = "/sword/{id}/name") public ResultBean findName(@PathVariable("id") Integer id) { return ResultHandler.ok(mSwordRepository.findById(id).get().getName()); }

查詢一個名稱.png
後記:捷文規範
1.本文成長記錄及勘誤表
專案原始碼 | 日期 | 備註 |
---|---|---|
V0.1--無 | 2018-10-19 | SpringBoot20-REST API介面 |
2.更多關於我
筆名 | 微信 | 愛好 | |
---|---|---|---|
張風捷特烈 | 1981462002 | zdl1994328 | 語言 |
我的github | 我的簡書 | 我的CSDN | 個人網站 |
3.宣告
1----本文由張風捷特烈原創,轉載請註明
2----歡迎廣大程式設計愛好者共同交流
3----個人能力有限,如有不正之處歡迎大家批評指證,必定虛心改正
4----看到這裡,我在此感謝你的喜歡與支援