面向資源的 Restful 風格的 api 接口本着簡潔,資源,便於擴展,便於理解等等各項優點,在現在的系統服務中愈來愈受歡迎。html
.net平臺有WebAPi項目是專門用來實現Restful api的,其良好的系統封裝,簡潔優雅的代碼實現,深受.net平臺開發人員所青睞,在後臺服務api接口中,已經逐步取代了輝煌一時MVC Controller,更準確地說,合適的項目使用更加合適的工具,開發效率將會更加高效。python
python平臺有tornado框架,也是原生支持了Restful api,在使用上有了很大的便利。web
Java平臺的SpringMVC主鍵在Web開發中取代了Struts2而佔據了更加有力的地位,咱們今天着重講解如何在Java SpringMVC項目中實現Restful api。spring
Restful api的實現脫離不了路由,這裏咱們的Restful api路由由spring mvc 的 controller來實現。chrome
開發環境:Windows 7 ×64 英文版json
Intellij IDEA 2017.2api
部署環境:JDK 1.8.0restful
Tomcat 8.5.5mvc
測試環境:chromeapp
fiddler
這裏的搭建步驟再也不贅述,若有須要參考,請導航至博客:http://www.cnblogs.com/qixiaoyizhan/p/5819392.html
爲了體現Restful api 咱們採用註解,RequestMapping("/api/Student")
具體的代碼以下:
1 package Controllers; 2 3 import org.springframework.web.bind.annotation.*; 4 5 @RestController 6 @RequestMapping("/api/Student") 7 public class StudentController { 8 9 @RequestMapping(method = RequestMethod.GET) 10 public String Get() { 11 return "{\"id\":\"1\",\"name\":\"1111111111\"}"; 12 } 13 14 @RequestMapping(method = RequestMethod.POST) 15 public String Post() { 16 return "{\"id\":\"2\",\"name\":\"2222222222\"}"; 17 } 18 19 @RequestMapping(method = RequestMethod.PUT) 20 public String Put() { 21 return "{\"id\":\"3\",\"name\":\"3333333333\"}"; 22 } 23 24 @RequestMapping(method = RequestMethod.DELETE) 25 public String DELETE() { 26 return "{\"id\":\"4\",\"name\":\"4444444444\"}"; 27 } 28 29 @RequestMapping(value = "/{id}",method = RequestMethod.GET) 30 public String Get(@PathVariable("id") Integer id) { 31 return "{\"id\":\""+id+"\",\"name\":\"get path variable id\"}"; 32 } 33 }
這裏有Get,Post,Put,Delete分別對應 查詢,添加,修改,刪除四種對資源的操做,即一般所說的CRUD。
spring mvc可實現restful的方式有@Controller和@RestController兩種方式,兩種方式的區別以下:
@Controller的方式實現若是要返回json,xml等文本,方法體上須要額外添加@ResponseBody註解,例如:
1 @ResponseBody //用於返回json數據或者text格式文本 2 @RequestMapping(value = "/TestJson", method = RequestMethod.GET) 3 public String TestJson() { 4 return "{\"id\":\"1001\",\"name\":\"zhangsan\"}"; 5 }
@RestController方式不須要寫@ResponseBody,可是不能返回模型綁定數據和jsp視圖,只能返回json,xml文本,僅僅是爲了更加方便返回json資源而已。
上述的Rest方法中多寫了個Get方法:
1 @RequestMapping(value = "/{id}",method = RequestMethod.GET) 2 public String Get(@PathVariable("id") Integer id) { 3 return "{\"id\":\""+id+"\",\"name\":\"get path variable id\"}"; 4 }
該方法能夠直接在url拼接一個參數,更加方便對資源的定向訪問,例如查一個student list 能夠默認空參數,而查詢對應的某一個student詳情信息,能夠id=studentId 定向查詢單條,使得咱們對資源的訪問更加快捷方便。
還有一種更加簡潔的寫法,Spring4.3中引進了{@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@PatchMapping}幾種寫法,讓接口的聲明更加地簡潔。下面代碼展現了用這種註解方式進行Rest接口的定義:
1 package Controllers; 2 3 import org.springframework.web.bind.annotation.*; 4 5 @RestController 6 @RequestMapping("/api/Student") 7 public class StudentController { 8 9 @GetMapping() 10 public String Get() { 11 return "{\"id\":\"1\",\"name\":\"1111111111\"}"; 12 } 13 14 @PostMapping() 15 public String Post() { 16 return "{\"id\":\"2\",\"name\":\"2222222222\"}"; 17 } 18 19 @PutMapping() 20 public String Put() { 21 return "{\"id\":\"3\",\"name\":\"3333333333\"}"; 22 } 23 24 @DeleteMapping() 25 public String DELETE() { 26 return "{\"id\":\"4\",\"name\":\"4444444444\"}"; 27 } 28 29 @GetMapping(value = "/{id}") 30 public String Get(@PathVariable("id") Integer id) { 31 return "{\"id\":\"" + id + "\",\"name\":\"get path variable id\"}"; 32 } 33 }
運行系統,使用fiddler調用restful api接口: