一直被老婆問RESTful 是什麼,我回答一直是一種代碼風格,Representational State Transfer,今天看了一位大神的回答表示自愧不如畢竟他老婆應該是new出來的,https://zhuanlan.zhihu.com/p/30396391。
看完之後,我就想了想簡單理解就是用HttpClent的各種請求來代替url中的add、update、delete等等,然後還要注意的就是冪等性,冪等並非RESTful 所要求的,是我們寫代碼的生活需要考慮的,可參見https://www.zhihu.com/question/27604206/answer/73460571,簡單來說就是要注意重複提交的問題,如何避免重複提交帶來的影響,容易產生重複提交的就是add操作對應的也就是POST操作,當然GET也可以進行add操作基於安全性考慮推薦用POST提交的數據放入post的body中。
基於以上就可以得出用GET代替get,用PATCH代替update,用POST代替add,用DELETE代替delete,那麼代碼就變成了
@RestController @RequestMapping("/data/hi") public class HelloController { @RequestMapping(value = "/",method = RequestMethod.GET) public String get() { return ""; } @RequestMapping(value = "/",method = RequestMethod.POST) public String add() { return ""; } @RequestMapping(value = "/",method = RequestMethod.DELETE) public String delete() { return ""; } @RequestMapping(value = "/",method = RequestMethod.PATCH) public String update() { return ""; }
看起來是不是更簡單了。