一、認識 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
Spring boot 開發 RESTFul 主要是幾個注解實現
1. @PathVariable
獲取 url 中的數據
現該注解是實現 RESTful 最主要的一個注解
2. @PostMapping
接收和處理 Post 方式的請求
3. @DeleteMapping
接收 delete 方式的請求,可以使用 GetMapping 代替
4. @PutMapping
接收 put 方式的請求,可以用 PostMapping 代替
5. @GetMapping
接收 get 方式的請求
三、案例
創建11-springboot-restful項目,一個基本的springboot項目
1. 實現
1. 建立一個model,里面有Student
package com.md.springboot.model;
/**
* @author MD
* @create 2020-08-21 20:20
*/
public class Student {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在StudentController中
package com.md.springboot.web;
import com.md.springboot.model.Student;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
/**
* @author MD
* @create 2020-08-21 20:21
*/
@RestController
public class StudentController {
@RequestMapping(value = "/student")
public Object student(Integer id , String name){
Student student = new Student();
student.setId(id);
student.setName(name);
return student;
}
@RequestMapping(value = "student/detail/{id}/{name}")
public Object student1(@PathVariable("id") Integer id,
@PathVariable("name") String name){
HashMap<Object, Object> retMap = new HashMap<>();
retMap.put("id",id);
retMap.put("name",name);
return retMap;
}
}
采用普通方式是這樣的:http://localhost:8080/student?id=1001&name=pony
若采用這種風格之后:http://localhost:8080/student/detail/1001/pony
可以根據開發中的需要,是否使用這樣的格式
2. 請求沖突的問題
如果在StudentController有這樣的請求
@RequestMapping(value = "student/detail/{id}/{name}")
public Object student1(@PathVariable("id") Integer id,
@PathVariable("name") String name){
HashMap<Object, Object> retMap = new HashMap<>();
retMap.put("id",id);
retMap.put("name",name);
return retMap;
}
// 和上面的請求路徑沖突
@RequestMapping(value = "student/detail/{id}/{status}")
public Object student2(@PathVariable("id") Integer id,
@PathVariable("status") String status){
HashMap<Object, Object> retMap = new HashMap<>();
retMap.put("id",id);
retMap.put("status",status);
return retMap;
}
此時運行會報錯,會出現路徑沖突
此時有兩種方式
- 修改請求路徑
- 修改請求方式
第一個就直接把路徑改了,肯定不會再沖突了,針對第二種方式,可以修改成這樣
@PostMapping(value = "student/detail/{id}/{name}")
public Object student1(@PathVariable("id") Integer id,
@PathVariable("name") String name){
Student student = new Student();
student.setId(id);
student.setName(name);
return student;
}
@GetMapping(value = "student/detail/{id}/{status}")
public Object student2(@PathVariable("id") Integer id,
@PathVariable("status") String status){
HashMap<Object, Object> retMap = new HashMap<>();
retMap.put("id",id);
retMap.put("status",status);
return retMap;
}
一個使用post請求,一個使用get請求,這樣就不會報錯了

/boot/queryOrder/1021/1(不推薦) -
分頁、排序等操作,不需要使用斜杠傳參數
例如:訂單列表接口
/boot/orders?page=1&sort=desc
一般傳的參數不是數據庫表的字段,可以不采用斜杠