REST簡介
REST(Representational State Transfer),表現形式狀態轉換
? ? ? ? 傳統風格資源描述形式
? ? ? ? ? ? ? ? http://localhost/user/getById?id=1
? ? ? ? ? ? ? ? http://localhost/user/saveUser
? ? ? ? REST風格描述形式
? ? ? ? ? ? ? ? http://localhost/user/1
? ? ? ? ? ? ? ? http://localhost/user
優點:
? ? ? ? 隱藏資源的訪問行為,無法通過地址得知對資源是何種操作
? ? ? ? 書寫簡化
按照REST風格訪問資源時使用行為動作區分對資源進行了何種操作
? ? ? ? http://localhost/users ? ? ? ? ? ? ? ?查詢全部用戶信息 ? ? ? ?GET(查詢)
? ? ? ? http://localhost/users/1 ? ? ? ? ? ? 查詢指定用戶信息 ? ? ? ?GET(查詢)
? ? ? ? http://localhost/users ? ? ? ? ? ? ? ?添加用戶信息 ? ? ? ? ? ? ? ?POST(新增/保存)
? ? ? ? http://localhost/users ? ? ? ? ? ? ? ?修改用戶信息 ? ? ? ? ? ? ? ?PUT(修改/更新)
? ? ? ? http://localhost/users/1 ? ? ? ? ? ? 刪除用戶信息 ? ? ? ? ? ? ? ?DELETE(刪除)
注:
上述行為是約定方式,約定不是規范,可以打破,所以稱REST風格,而不是REST規范
描述模塊的名稱通常使用復數,也就是加s的格式描述,表示此類資源,而非單個資源,例如:users、books...
RESTful
根據REST風格對資源進行訪問稱為RESTful
直接看映射路徑處的代碼如下(get查詢)
//http://localhost/user/100 =>根據id查詢@GetMapping("/user/{id}/{xxx}")public String findUserById(@PathVariable("id") int id,@PathVariable("xxx") String yyy){System.out.println("id==>"+id);System.out.println("yyy==>"+yyy);return "/index.jsp";}