目錄
1、對(duì)于json請(qǐng)求體參數(shù),
2、對(duì)于路徑傳參
3、對(duì)于query傳參
4、對(duì)于form-data參數(shù),
總結(jié):
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-818690.html
1、對(duì)于json請(qǐng)求體參數(shù),
請(qǐng)求頭的Content-Type應(yīng)為application/json。在前端,可以使用data格式傳參。在后端,可以使用@RequestBody注解來(lái)接收參數(shù)。
this.$axios({
method: 'post',
url: 'http://localhost:8080/api/upload/editGoods',
data: {
id: this.id,
name: this.name,
price: this.price
}
}).then((res) => {
this.$message({
message: '修改成功',
type: 'success'
})
})
@GetMapping("/editGoods")
public Result editGoods(@RequestBody Goods goods) {
return uploadFileService.editGoods(goods);
}
?
2、對(duì)于路徑傳參
(例如:test/111/2222),請(qǐng)求頭不需要設(shè)置Content-Type。在前端,可以將參數(shù)通過(guò)URL的方式傳遞(例如:url=/api/upload/test2/111/2222)。在后端,可以使用@PathVariable注解來(lái)接收參數(shù)。
this.$axios({
method: 'post',
url: 'http://localhost:8080/api/user/deleteUser/' + userId,
}).then((res) => {
this.$message({
message: res.data.message,
type: "success",
});
// 刷新表格數(shù)據(jù)
this.selectUser();
});
}).catch(() => {
// 用戶點(diǎn)擊了取消按鈕
// 執(zhí)行取消操作或不執(zhí)行任何操作
});
@PostMapping("/deleteUser/{userId}")
public Result deleteUser(@PathVariable String userId) {
return userservice.deleteUser(userId);
}
3、對(duì)于query傳參
(例如:test3?id=11111&name=222222),請(qǐng)求頭也不需要設(shè)置Content-Type。在前端,可以將參數(shù)通過(guò)URL的方式傳遞(例如:url=/api/upload/test3?id=11111&name=222222)。在后端,可以使用@RequestParam注解來(lái)接收參數(shù)。
this.$axios({
method: 'post',
url: 'http://localhost:8080/api/user/deleteUser',
params: {
userId: userId
}
}).then((res) => {
this.$message({
message: res.data.message,
type: "success",
});
// 刷新表格數(shù)據(jù)
this.selectUser();
}).catch(() => {
// 用戶點(diǎn)擊了取消按鈕
// 執(zhí)行取消操作或不執(zhí)行任何操作
});
//params傳參
@GetMapping("/editGoods");
public String editGoods(@RequestParam String id, @RequestParam String name) {
System.out.println(id);
System.out.println(name);
return id;
}
4、對(duì)于form-data參數(shù),
請(qǐng)求頭的Content-Type應(yīng)為multipart/form-data。在前端,可以使用params格式傳參。在后端,可以使用@RequestParam注解來(lái)接收參數(shù)。
?
this.$axios({
method: 'post',
url: 'http://localhost:8080/api/upload/editGoods',
params: {
id: this.id,
name: this.name,
}
}).then((res) => {
this.$message({
message: '修改成功',
type: 'success'
})
})
//params傳參
@GetMapping("/editGoods");
public String editGoods(@RequestParam String id, @RequestParam String name) {
System.out.println(id);
System.out.println(name);
return id;
}
?query傳參和form-data傳參,后端接收是一樣的文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-818690.html
總結(jié):
- - form-data參數(shù)使用multipart/form-data作為Content-Type,前端使用params格式傳參,后端使用@RequestParam注解接收參數(shù)。
- - json請(qǐng)求體參數(shù)使用application/json作為Content-Type,前端使用data格式傳參,后端使用@RequestBody注解接收參數(shù)。
- - 路徑傳參不需要設(shè)置Content-Type,前端將參數(shù)通過(guò)URL傳遞,后端使用@PathVariable注解接收參數(shù)。
- - query傳參也不需要設(shè)置Content-Type,前端將參數(shù)通過(guò)URL傳遞,后端使用@RequestParam注解接收參數(shù)。
到了這里,關(guān)于在 Spring MVC 中,用于接收前端傳遞的參數(shù)的注解常用的有以下幾種的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!