【问题标题】:'delete' not supported spring boot“删除”不支持弹簧启动
【发布时间】:2020-03-30 22:31:10
【问题描述】:

使用 Angular 8 和 Springboot 应用程序...通过 JSON 传递值得到错误: 不支持请求删除 这是错误:

这个应用程序没有明确的 /error 映射,所以你看到 这是一个后备。

Thu Dec 05 22:55:04 WET 2019 出现意外错误 (类型=不允许的方法,状态=405)。请求方法 'GET' 不 支持

 @DeleteMapping("/Emp/{id}")
        public boolean deleteEmployee(@PathVariable Long id) {
         collaborateurRepository.deleteById(id);
            return true;
        }

组件.ts

 deleteEmployee() {
    this.employeservice.deleteEmployee(this.employee.id)
      .subscribe(data => console.log(data), error => console.log(error));

    this.gotoList();
  }

服务:

 deleteEmployee( id: number): Observable<Object> {
      return this.http.delete(`http://localhost:8080/api/Emp/`+id);
    }

【问题讨论】:

  • 我试过这个但徒劳无功@RequestMapping(value="/Emp/{id}", method=RequestMethod.DELETE) public boolean deleteEmployee(@PathVariable Long id) {collectionurRepository.deleteById(id) ;返回真; }
  • 请分享您调用此 API 端点的 Angular 代码
  • @MattU component.ts deleteEmployee() { this.employeservice.deleteEmployee(this.employee.id) .subscribe(data => console.log(data), error => console.log(error )); this.gotoList(); } 服务:deleteEmployee(id: number): Observable { return this.http.delete(http://localhost:8080/api/Emp/+id); }
  • 我们需要查看 employeeservice.deleteemployee 方法,或者任何调用 Angular HttpClient 的方法。请使用代码编辑您的问题,而不是使用评论。
  • @MattU 我编辑它,首先它应该在服务器上工作,然后我会在客户端看到

标签: javascript angular spring-boot request http-delete


【解决方案1】:

看起来您正在调用 REST-Api 来删除带有 HTTP-GET 请求的条目。您必须使用 HTTP-DELETE 调用此端点,这将防止 Method not allowed 错误。假设您的 UI 有一个类似 get(/Emp/123) 的调用,它应该是 delete(/Emp/123)

【讨论】:

  • U 的意思是@RequestMapping(value="/Emp/{id}", method=RequestMethod.GET) public boolean deleteEmployee(@PathVariable Long id) { CollaborationRepository.deleteById(id);返回真; }
  • 不,我现在看到了你的评论。你可以在布尔返回类型之前添加一个@ResponseBody 像:@DeleteMapping("/Emp/{id}") public @ResponseBody boolean deleteEmployee(@PathVariable Long id) { collaborateurRepository.deleteById(id); return true; } 或这个:@RequestMapping(value="/Emp/{id}", method=RequestMethod.DELETE) public @ResponseBody boolean deleteEmployee(@PathVariable Long id) { collaborateurRepository.deleteById(id); return true; }
  • @Kindth 请更新您的问题以包含调用此 API 端点的角度代码
【解决方案2】:

我找到了答案,除了删除方法之外,我还必须添加 Get 方法 谢谢各位:)

 @RequestMapping(value="/empdelete/{id}", method= {RequestMethod.DELETE, RequestMethod.GET})
 public boolean deleteEmployee( @PathVariable("id") Long id) {
     collaborateurRepository.deleteById(id);
        return true;
    }

【讨论】:

  • 通常情况下,您不必这样做。如果这行得通,这意味着在您的 Angular 代码的某个地方,您正在发送 GET 请求而不是 DELETE。由于这在您的代码中不存在,因此很难帮助您正确解决您的问题。
  • @g00glen00b 我认为这很正常,因为我只是从数组中恢复 id 所以我用它来删除选定的员工所以我要同时使用它们!
猜你喜欢
  • 2018-09-08
  • 2022-01-15
  • 2015-09-17
  • 2018-07-26
  • 1970-01-01
  • 2015-01-22
  • 2022-01-04
  • 2018-04-27
  • 1970-01-01
相关资源
最近更新 更多