認識 RESTFul
REST(英文:Representational State Transfer,簡稱REST)
一種互聯網軟件架構設計的風格,但它并不是標準,它只是提出了一組客戶端和服務器交互時的架構理念和設計原則,基于這種理念和原則設計的接口可以更簡潔,更有層次,REST這個詞,是Roy Thomas Fielding在他2000年的博士論文中提出的。
任何的技術都可以實現這種理念,如果一個架構符合REST原則,就稱它為RESTFul架構。
比如我們要訪問一個http接口:http://localhost:8080/boot/order?id=1021&status=1
采用RESTFul風格則http地址為:http://localhost:8080/boot/order/1021/1
Spring boot開發RESTFul 主要是幾個注解實現:
@PathVariable:獲取url中的數據,該注解是實現RESTFul最主要的一個注解。
@PostMapping:接收和處理Post方式的請求
@DeleteMapping:接收delete方式的請求,可以使用GetMapping代替
@PutMapping:接收put方式的請求,可以用PostMapping代替
@GetMapping:接收get方式的請求
• 輕量,直接基于http,不再需要任何別的諸如消息協議,get/post/put/delete為CRUD操作
• 面向資源,一目了然,具有自解釋性。
• 數據描述簡單,一般以xml,json做數據交換。
• 無狀態,在調用一個接口(訪問、操作資源)的時候,可以不用考慮上下文,不用考慮當前狀態,極大的降低了復雜度。
• 簡單、低耦合
項目名稱:014-springboot-restful
該項目集成了MyBatis、spring、SpringMVC,通過模擬實現對學生的增刪改查操作。
1.創建RESTfulController,并編寫代碼
@RestController
public class RESTfulController {
/**
* 添加學生
* 請求地址:http://localhost:9090/014-springboot-restful/springBoot/student/wangpeng/23
* 請求方式:POST
* @param name
* @param age
* @return
*/
@PostMapping(value = "/springBoot/student/{name}/{age}")
public Object addStudent(@PathVariable("name") String name,
@PathVariable("age") Integer age) {
Map retMap = new HashMap();
retMap.put("name",name);
retMap.put("age",age);
return retMap;
}
/**
* 刪除學生
* 請求地址:http://localhost:9090/014-springboot-restful/springBoot/student/1
* 請求方式:Delete
* @param id
* @return
*/
@DeleteMapping(value = "/springBoot/student/{id}")
public Object removeStudent(@PathVariable("id") Integer id) {
return "刪除的學生id為:" + id;
}
/**
* 修改學生信息
* 請求地址:http://localhost:9090/014-springboot-restful/springBoot/student/2
* 請求方式:Put
* @param id
* @return
*/
@PutMapping(value = "/springBoot/student/{id}")
public Object modifyStudent(@PathVariable("id") Integer id) {
return "修改學生的id為" + id;
}
@GetMapping(value = "/springBoot/student/{id}")
public Object queryStudent(@PathVariable("id") Integer id) {
return "查詢學生的id為" + id;
}
}
2.使用Postman模擬發送請求,進行測試
3.總結:其實這里我們能感受到的好處
• 傳遞參數變簡單了
• 服務提供者對外只提供了一個接口服務,而不是傳統的CRUD四個接口
項目名稱:015-springboot-restful-url-conflict
• 改路徑
• 改請求方式
創建RESTfulController類,結合Postman進行測試說明
@RestController
public class RESTfulController {
/**
* id:訂單標識
* status:訂單狀態
* 請求路徑:http://localhost:9090/015-springboot-restful-url-conflict/springBoot/order/1/1001
* @param id
* @param status
* @return
*/
@GetMapping(value = "/springBoot/order/{id}/{status}")
public Object queryOrder(@PathVariable("id") Integer id,
@PathVariable("status") Integer status) {
Map map = new HashMap();
map.put("id",id);
map.put("status",status);
return map;
}
/**
* id:訂單標識
* status:訂單狀態
* 請求路徑:http://localhost:9090/015-springboot-restful-url-conflict/springBoot/1/order/1001
* @param id
* @param status
* @return
*/
@GetMapping(value = "/springBoot/{id}/order/{status}")
public Object queryOrder1(@PathVariable("id") Integer id,
@PathVariable("status") Integer status) {
Map map = new HashMap();
map.put("id",id);
map.put("status",status);
return map;
}
/**
* id:訂單標識
* status:訂單狀態
* 請求路徑:http://localhost:9090/015-springboot-restful-url-conflict/springBoot/1001/order/1
* @param id
* @param status
* @return
*/
@GetMapping(value = "/springBoot/{status}/order/{id}")
public Object queryOrder2(@PathVariable("id") Integer id,
@PathVariable("status") Integer status) {
Map map = new HashMap();
map.put("id",id);
map.put("status",status);
return map;
}
/**
* id:訂單標識
* status:訂單狀態
* 請求路徑:http://localhost:9090/015-springboot-restful-url-conflict/springBoot/1001/order/1
* @param id
* @param status
* @return
*/
@PostMapping(value = "/springBoot/{status}/order/{id}")
public Object queryOrder3(@PathVariable("id") Integer id,
@PathVariable("status") Integer status) {
Map map = new HashMap();
map.put("id",id);
map.put("status",status);
return map;
}
/**
* query1和query2兩個請求路徑會發生請求路徑沖突問題
* query3與query1和query2發生請求沖突
* 注意:雖然兩個路徑寫法改變了,但是由于傳遞的兩個參數都是int值,所以不知道該交給哪個請求進行處理
* 就會出現匹配模糊不清的異常,所以要想解決沖突,有兩種方式:
* 1.修改請求路徑
* 2.修改請求方式
*/
}
• 增post請求、刪delete請求、改put請求、查get請求
• 請求路徑不要出現動詞
例如:查詢訂單接口
/boot/order/1021/1(推薦)
/boot/queryOrder/1021/1(不推薦)
• 分頁、排序等操作,不需要使用斜杠傳參數
例如:訂單列表接口 /boot/orders?page=1&sort=desc
一般傳的參數不是數據庫表的字段,可以不采用斜杠