前端使用delete方法訪問接口,后端使用@DeleteMapping注解,方法內(nèi)使用@PathVariable接參,注意注解中的: /{id} 的用法
后端接口:
// 根據(jù)ID刪除用戶
@DeleteMapping("/api/deleteUserById/{id}")
@ResponseBody
public String deleteUserById(@PathVariable("id") Integer id) {
int rowsDeleted = userMapper.deleteUserById((long)id);
if (rowsDeleted > 0) {
return "數(shù)據(jù)刪除成功!";
} else {
return "數(shù)據(jù)刪除失??!";
}
}
前端接口:
axios.delete(‘http://127.0.0.1:8080/api/deleteUserById/’+id)
.then(response => {
// 處理成功響應(yīng)
console.log(‘刪除成功’, response);
})
.catch(error => {
// 處理錯(cuò)誤響應(yīng)
console.error(‘刪除失敗’, error);
});文章來源:http://www.zghlxwxcb.cn/news/detail-610296.html
秋風(fēng)寫于淄博,java學(xué)習(xí)筆記,由于java是新手,歡迎高手批評指正!文章來源地址http://www.zghlxwxcb.cn/news/detail-610296.html
到了這里,關(guān)于使用axios請求@DeleteMapping注解的接口的方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!