Angular 8的删除方法未调用Spring Boot的删除方法。

huangapple go评论90阅读模式
英文:

Angular 8 delete method is not calling spring boot delete method

问题

我的 Angular 8 删除方法没有调用 Spring Boot 的删除方法(日志中没有打印任何内容)。

Angular 8 删除方法:

deleteStudent(id: number): Observable<any> {
    console.log(`${this.baseUrl}/deleteById/${id}`);
    return this.httpClient.delete(`${this.baseUrl}/deleteById/${id}`, { responseType: 'text' });    
}

Spring Boot 删除方法(REST API):

@DeleteMapping("/deleteById/{id}")
@ResponseBody
ResponseEntity<?> deleteById(@PathVariable("id") long id){
    try{
        studentService.deleteById(id);
        LOGGER.info("记录成功删除");
        return new ResponseEntity<>(HttpStatus.OK);
    }catch (Exception e){
        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

在控制台打印 Angular 的 URL,例如:http://localhost:8888/deleteById/55,我将其复制并在 Postman 中使用,特定记录被成功删除。这个问题只出现在删除方法中,其他方法都正常工作。

英文:

My angular 8 delete method is not calling spring boot delete method (nothing prints in log)

angular 8 delete method :

deleteStudent(id: number): Observable&lt;any&gt; {
    console.log(`${this.baseUrl}/deleteById/${id}`);
    return this.httpClient.delete(`${this.baseUrl}/deleteById/${id}`, { responseType: &#39;text&#39; });    
  }

spring boot delete method (REST API) :

@DeleteMapping(&quot;/deleteById/{id}&quot;)
    @ResponseBody
    ResponseEntity&lt;?&gt; deleteById(@PathVariable(&quot;id&quot;) long id){
        try{
            studentService.deleteById(id);
            LOGGER.info(&quot;record successfully deleted&quot;);
            return new ResponseEntity&lt;&gt;(HttpStatus.OK);
        }catch (Exception e){
           
            return new ResponseEntity&lt;&gt;(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

after printing the angular url eg: http://localhost:8888/deleteById/55 in console I copied it and used in postman, the particular record deleted successfully. This problem only occurs for delete method, another methods are perfectly work.

答案1

得分: 0

这是我的错误,我没有在服务类中订阅删除方法。

英文:

It was my mistake I haven't subscribe to the delete method in service class.

huangapple
  • 本文由 发表于 2020年4月6日 17:10:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/61056377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定