今天在學習elasticsearch時,遇到一個問題:專案中前端采用的是Vue2+axios,后端的介面采用Restful風格來接收:
關于Resultful風格:
1. GET(SELECT):從服務器取出資源(一項或多項);
2. POST(CREATE):在服務器新建一個資源;
3. PUT(UPDATE):在服務器更新資源(客戶端提供改變后的完整資源);
4. PATCH(UPDATE):在服務器更新資源(客戶端提供改變的屬性);
5. DELETE(DELETE):從服務器洗掉資源;
6. HEAD:獲取資源的元資料;
7. OPTIONS:獲取資訊,關于資源的哪些屬性是客戶端可以改變的,
前端代碼
//axios.post("/hotel/list", params)//這是采用的post方式(剛開始是這樣的,我將post改為get后,便會報錯)
axios.get("/hotel/list",{//這是采用get方式,這兩種方式最后結果都正確,后面會說具體怎么做的
params:params
})
.then(resp => {
this.hotels = resp.data.hotels;
this.total = resp.data.total;
this.totalPage = Math.floor((this.total + 5 - 1) / 5);
if (location) {
this.setMapCenter(location);
} else if(this.hotels && this.hotels.length > 0){
this.setMapCenter(this.hotels[0].location);
}
this.initMarker();
})
.catch(err => {
console.log(err)
this.hotels = []
})
可以看到一般要查詢資料時是采用GetMapping接收,但是在學習的原始碼中發現,查詢時向后端的請求時POST方式,后端也是PostMapping接收引數,當我將POST方法改為GET方法時又會報空指標的錯誤(前端傳遞的引數后端沒有接收到)為什么改個傳遞方式后,結果就傳不進去了?隨后我便查找了axios的官網
請求方式別名 為了方便起見,已經為所有支持的請求方法提供了別名, axios.request(config) axios.get(url[, config]) axios.delete(url[, config]) axios.head(url[, config]) axios.options(url[, config]) axios.post(url[, data[, config]]) axios.put(url[, data[, config]]) axios.patch(url[, data[, config]])
可以看到,axios.post("/hotel/list", params),Post方法是可以直接傳參的,但是Get方法卻不能直接傳參,需要在配置中(config)配置,
具體的傳遞引數方法案例如下:
一、Get請求
1.1基礎型別接收,名字對應就可以
// method
const params = {
id: '123456789',
name: '張三'
}
test(params)
// api
export function test (params) {
return axios({
url: url,
method: 'GET',
params: params
})
}
// 后臺
@GetMapping("/test")
public Result test(Long id, String name) {
return Res.ok();
}
1.2使用Map接收,需要添加 RequestParam 注解
// method
const params = {
id: '123456789',
name: '張三'
}
test(params)
// api
export function test (params) {
return axios({
url: url,
method: 'GET',
params: params
})
}
// 后臺
@GetMapping("/test")
public Result test(@RequestParam Map<String, Object> map) {
return Res.ok();
}
1.3物體類接收
// 物體類
@Data
public class TestEntity {
Long id;
String name;
}
// method
const params = {
id: '123456789',
name: '張三'
}
test(params)
// api
export function test (params) {
return axios({
url: url,
method: 'GET',
params: params
})
}
// 后臺
@GetMapping("/test")
public Result test(TestEntity testEntity) {
return Res.ok();
}
二、Post請求
2.1基礎型別接收,名稱對應即可
// method
const params = {
id: '123456789',
name: '張三'
}
test(params)
// api
export function test (params) {
return axios({
url: url,
method: 'POST',
params: params
})
}
// 后臺
@PostMapping("/test")
public Result test(Long id, String name) {
return Res.ok();
}
2.2使用map接收
// method
const params = {
id: '123456789',
name: '張三'
}
test(params)
// api
export function test (params) {
return axios({
url: url,
method: 'POST',
params: params
})
}
// 后臺
@PostMapping("/test")
public Result test(@RequestParam Map<String, Object> map) {
return Res.ok();
}
2.3使用物體類接收(params傳遞引數)
// 物體類
@Data
public class TestEntity {
Long id;
String name;
}
// method
const params = {
id: '123456789',
name: '張三'
}
test(params)
// api
export function test (params) {
return axios({
url: url,
method: 'POST',
params: params
})
}
// 后臺
@PostMapping("/test")
public Result test(TestEntity testEntity) {
return Res.ok();
}
2.4物體類接收(data傳遞引數)
// 物體類
@Data
public class TestEntity {
Long id;
String name;
}
// method
const params = {
id: '123456789',
name: '張三'
}
test(params)
// api
export function test (params) {
return axios({
url: url,
method: 'POST',
data: params
})
}
@PostMapping("/test")
public Result test(@RequestBody TestEntity testEntity) {
return Res.ok();
}
總結
總體來說,只要使用 params get與post請求基本是一樣使用的,如果引數名與傳遞名稱不一致,需要使用@RequestParam修飾.
若使用Map接收引數,必須使用@RequestParam修飾,但是如果想傳list型別的資料,需要使用單獨的方法處理,
若使用data傳遞引數,必須使用一個物體類接收引數,而且需要添加注解@RequestBody進行修飾
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/551123.html
標籤:其他
下一篇:返回列表
