點擊上方藍色字體,選擇「標星公眾號」
優質文章,第一時間送達
作者 | 山丘i
來源 | urlify.cn/iyQrym
一、認識 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 主要是幾個註解實現
2.1 @PathVariable獲取 url 中的數據
現該註解是實現 RESTful 最主要的一個註解
接收和處理 Post 方式的請求
2.3 @DeleteMapping接收 delete 方式的請求,可以使用 GetMapping 代替
2.4@PutMapping接收 put 方式的請求,可以用 PostMapping 代替
2.5 @GetMapping接收 get 方式的請求
三、案例創建11-springboot-restful項目,一個基本的springboot項目
3.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
可以根據開發中的需要,是否使用這樣的格式
3.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
一般傳的參數不是資料庫表的欄位,可以不採用斜槓粉絲福利:Java從入門到入土學習路線圖
👇👇👇
感謝點讚支持下哈