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