返回viewmodel或空实体的spring CRUD DELETE操作

返回viewmodel或空实体的spring CRUD DELETE操作,spring,spring-boot,spring-boot-2,Spring,Spring Boot,Spring Boot 2,我想写一个删除操作,如果不存在id错误,它将返回一个无内容的正文。如果id不存在,我想重定向到响应的GET视图 控制器代码: 有关视图的更多信息: GET视图只是一个显示对应于id的todo实体的视图。删除是通过使用ajax调用DELETE方法的按钮进行的。然后响应返回为204,正文中没有内容,我用javascript将用户重定向到主页。。。如果DELETE方法中不存在id,我想重定向到GET方法以显示错误消息 如果有人有这样的想法。 提前感谢。尝试使用返回类型作为ResponseEntity和

我想写一个删除操作,如果不存在id错误,它将返回一个无内容的正文。如果id不存在,我想重定向到响应的GET视图

控制器代码:

有关视图的更多信息:

GET视图只是一个显示对应于id的todo实体的视图。删除是通过使用ajax调用DELETE方法的按钮进行的。然后响应返回为204,正文中没有内容,我用javascript将用户重定向到主页。。。如果DELETE方法中不存在id,我想重定向到GET方法以显示错误消息

如果有人有这样的想法。 提前感谢。

尝试使用返回类型作为ResponseEntity和响应主体以及响应状态。请参阅以下代码更改:

@RequestMapping(value = "/todo/delete/{id}", method = RequestMethod.DELETE)
public ResponseEntity deleteTodo(@PathVariable String id, RedirectAttributes redirAttrs)
{
   boolean exists = todoRepository.existsById(Long.decode(id));

   if (exists) {
       todoRepository.deleteById(Long.decode(id));
       return new ResponseEntity(HttpStatus.NO_CONTENT); //This will return No Content status
   }
   else {
       redirAttrs.addFlashAttribute("msginfo", "ctl-todo.delete.msginfo.id-not-exist");
       redirAttrs.addFlashAttribute("requestedId", id);

       return new ResponseEntity( "redirect:/todo/delete" + id, HttpStatus.OK);
   }
}
我的最终答案是:

@RequestMapping(value = "/todo/delete/{id}", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteTodo(@PathVariable String id, RedirectAttributes redirAttrs)
{
   boolean exists = todoRepository.existsById(Long.decode(id));

   if (exists) {
       todoRepository.deleteById(Long.decode(id));
       return new ResponseEntity<>(HttpStatus.NO_CONTENT);
   }
   else {
       redirAttrs.addFlashAttribute("msginfo", "ctl-todo.delete.msginfo.id-not-exist");
       redirAttrs.addFlashAttribute("requestedId", id);
       /* I use CONFLICT here to explain that the entity was possibly deleted
          by another user between the moment the user give the view containing
          the DELETE ajax link and the moment he click on it. */
       return new ResponseEntity<String>( "redirect:/todo/delete" + id, HttpStatus.CONFLICT);
   }
}

感谢Mandar Dharurkar&Jeethesh Kotian的帮助

你应该遵循休息原则。如果存在id,则使用204删除没有内容的内容是正确的。如果id不存在,抛出异常404。UID上的句柄404有这样的例子吗?或者像github项目那样做?现在对我来说有点复杂。好吧@BeRoots,我已经添加了一个答案,它将直接重定向到索引,而没有内容响应。希望这会有帮助!使用Responeneity将删除该模型。是的,但正如中所述,它使您能够更好地控制所使用的响应状态,并且您可以拥有所需的任何响应主体,而无需事先指定返回类型。同意!但在这种特殊情况下,响应是ViewModelJSP页面。因此,回答是不够的。我已经发布了最终答案。我将在2天内标记为解决问题的anwser。。。
@RequestMapping(value = "/todo/delete/{id}", method = RequestMethod.DELETE)
public ResponseEntity<?> deleteTodo(@PathVariable String id, RedirectAttributes redirAttrs)
{
   boolean exists = todoRepository.existsById(Long.decode(id));

   if (exists) {
       todoRepository.deleteById(Long.decode(id));
       return new ResponseEntity<>(HttpStatus.NO_CONTENT);
   }
   else {
       redirAttrs.addFlashAttribute("msginfo", "ctl-todo.delete.msginfo.id-not-exist");
       redirAttrs.addFlashAttribute("requestedId", id);
       /* I use CONFLICT here to explain that the entity was possibly deleted
          by another user between the moment the user give the view containing
          the DELETE ajax link and the moment he click on it. */
       return new ResponseEntity<String>( "redirect:/todo/delete" + id, HttpStatus.CONFLICT);
   }
}