如何使用Spring Boot在API delete方法中传递多个ID
我使用的是spring boot 1.5。我试图在DELETE中传递多个ID,因此我尝试传递一个ID列表并浏览它们以应用DELETE方法,但它不是word,请帮助如何使用Spring Boot在API delete方法中传递多个ID,spring,rest,api,Spring,Rest,Api,我使用的是spring boot 1.5。我试图在DELETE中传递多个ID,因此我尝试传递一个ID列表并浏览它们以应用DELETE方法,但它不是word,请帮助 @RequestMapping(value = "/deleteAlert/{ids}", method = RequestMethod.DELETE) ResponseEntity<Void> massiveDelete(@PathVariable List<Long> ids ){ for (Lon
@RequestMapping(value = "/deleteAlert/{ids}", method = RequestMethod.DELETE)
ResponseEntity<Void> massiveDelete(@PathVariable List<Long> ids ){
for (Long id : ids) {
alertService.deleteAlert(id);}
return ok().build();
}
@RequestMapping(value=“/deleteAlert/{ids}”,method=RequestMethod.DELETE)
ResponseEntity massiveDelete(@PathVariable列表ID){
用于(长id:id){
alertService.deleteAlert(id);}
返回ok().build();
}
因此,删除具有选定ID的所有警报您可以将ID作为参数发送,例如
deleteAlert/ids=ID1&ids=ID2&ids=ID3
请注意,这些参数具有相同的名称“ids”
然后将它们作为字符串列表获取
@RequestMapping(value = "/deleteAlert", method = RequestMethod.DELETE)
ResponseEntity massiveDelete(@RequestParam("ids") List<String> idsList)
{
for (String id : idsList) {
alertService.deleteAlert(Long.parseLong(id));
}
return ok().build();
}
@RequestMapping(value=“/deleteAlert”,method=RequestMethod.DELETE)
ResponseEntity massiveDelete(@RequestParam(“ids”)列表idsList)
{
用于(字符串id:idsList){
alertService.deleteAlert(Long.parseLong(id));
}
返回ok().build();
}
我不确定是否正确解决了您的问题,也许可以具体说明哪些问题不起作用
但如果我没有弄错,您希望将ID列表传递给删除端点
按照你的实际操作方法,我刚刚用curl测试了一下
守则:
@控制器
公共类控制器示例{
@RequestMapping(value=“/deleteAlert/{ids}”,method=RequestMethod.DELETE)
公共响应massiveDelete(@PathVariable列表ID){
用于(长id:id){
系统输出打印项次(id);
}
返回ok().build();
}
}
请求:
curl-X DELETE“localhost:8080/deleteAlert/1,2”
输出:
2019-09-13 19:15:12.156 INFO 27347 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 27 ms
1
2
因此,api调用可以工作。那么,您的问题可能来自调用api的方式?非常感谢您的帮助,但我需要从后端获取Id列表,因此我只是想了解如何在每个给定Id(键入Long)上应用方法delete(此方法仅针对一个给定Id实现)答案会以字符串列表的形式检索ID列表。您只需将id作为参数添加到前端
/deleteAlert/ids=[first\u id]&ids=[second\u id]
我已编辑了答案,以显示如何将id应用于deleteAlert()方法。