【问题标题】:PUT request from Angular5 to spring-boot来自 Angular5 的 PUT 请求到 spring-boot
【发布时间】:2018-06-08 23:44:35
【问题描述】:

如您所知,put 请求如下所示:

put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>

所以在我的情况下,它看起来像这样:

updateIntervention(id:number,idDev:number){
    if(this.authService.getToken()==null) {
      this.authService.loadToken();
    }
    return this.http.put(this.host+"/updateIntervention/"+id,idDev,{headers:new HttpHeaders({'Authorization':this.authService.getToken()})});
  }

所以在 spring-data(back end) 中,等待 PUT 请求的方法应该是这样的,例如:

@RequestMapping(value="/updateIntervention/{id}",method = RequestMethod.PUT)
 public Intervention update(@PathVariable Long id , @RequestBody B obj){

 ... 
}

所以这里的 obj 是 B 类的对象,它具有 Long 类型的属性,它将返回 angular5 发送的 PUT 请求的主体。

我的问题是我一直在 spring-data 中创建这些不同的类(因为有时我在 body 中只发送一个属性,这就是为什么我创建一个新类来表示它)所以我可以得到由 angular 发送的 body 甚至认为它仅包含一个属性,如本例中的 idDev。

有没有更好的方法来避免在 spring-data 中创建新类,所以我无法在 body 中获取前端发送的 idDev?

如果我只是举例:

public Intervention update(@PathVariable Long id , @RequestBody long idDev){

我得到反序列化等错误..

有什么想法还是不可能?

【问题讨论】:

    标签: java spring spring-data angular5


    【解决方案1】:

    将您的参数设置为form-data 并使用@RequestParam 将其放入您的控制器中:

    public Intervention update(@PathVariable Long id , @RequestParam long idDev){
    

    【讨论】:

    • 感谢您的回答:),表单数据是什么意思?
    • 是的。由于您只传递一个请求参数,因此无需在那里使用 RequestBody
    • 啊,好吧,但是表单数据是什么意思?和我在 angular5 put request 中的方式一样吗?
    猜你喜欢
    • 1970-01-01
    • 2021-01-10
    • 2020-11-27
    • 2012-09-14
    • 2014-05-15
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多