【前言】
面向資源的 Restful 風格的 api 接口本着簡潔,資源,便於擴展,便於理解等等各項優勢,在如今的系統服務中越來越受歡迎。
.net平台有WebAPi項目是專門用來實現Restful api的,其良好的系統封裝,簡潔優雅的代碼實現,深受.net平台開發人員所青睞,在后台服務api接口中,已經逐步取代了輝煌一時MVC Controller,更准確地說,合適的項目使用更加合適的工具,開發效率將會更加高效。
python平台有tornado框架,也是原生支持了Restful api,在使用上有了很大的便利。
Java平台的SpringMVC主鍵在Web開發中取代了Struts2而占據了更加有力的地位,我們今天着重講解如何在Java SpringMVC項目中實現Restful api。
【實現思路】
Restful api的實現脫離不了路由,這里我們的Restful api路由由spring mvc 的 controller來實現。
【開發及部署環境】
開發環境:Windows 7 ×64 英文版
Intellij IDEA 2017.2
部署環境:JDK 1.8.0
Tomcat 8.5.5
測試環境:chrome
fiddler
【實現過程】
1、搭建spring mvc maven項目
這里的搭建步驟不再贅述,如有需要參考,請導航至博客:http://www.cnblogs.com/qixiaoyizhan/p/5819392.html
2、新建控制器 StudentController
為了體現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接口:
1.Get方式
2.Post方式
3.Put方式
4.Delete方式
5.Get/id方式