Angular 弹簧靴&x2B;角度:所需参数不存在

Angular 弹簧靴&x2B;角度:所需参数不存在,angular,spring-boot,Angular,Spring Boot,在我的项目中,在客户端,在angular中,我使用以下代码执行POST请求,并基于Spring Boot将对象数组传递给服务器 const headers = new HttpHeaders().set( "Content-Type", "application/json; charset=utf-8" ); var params: HttpParams = new HttpParams(); params.set("allQuest

在我的项目中,在客户端,在angular中,我使用以下代码执行POST请求,并基于Spring Boot将对象数组传递给服务器

      const headers = new HttpHeaders().set(
      "Content-Type",
      "application/json; charset=utf-8"
    );

    var params: HttpParams = new HttpParams();
    params.set("allQuestions", JSON.stringify(this.dataSource));

    this.http
      .post("/questions/import", params, { headers: headers })
      .subscribe(
        (data) => {
          this.dialog.close(true);
        },
        (error) => this.displayErrorMessage(error)
      );
在服务器(Spring Boot)上,有一种方法可以接收此请求:

    @RequestMapping(path = "/questions/import", method = RequestMethod.POST,
        consumes = "application/json",
        produces = "application/json")
 public ResponseEntity<String> importQuestions(@RequestParam("allQuestions") Question[] allQuestions) { ...  }
@RequestMapping(path=“/questions/import”,method=RequestMethod.POST,
consumes=“application/json”,
products=“application/json”)
公共响应重要问题(@RequestParam(“allQuestions”)问题[]所有问题){…}
从客户端发出POST请求后,我在服务器端收到以下错误:

已解决[org.springframework.web.bind.MissingServletRequestParameterException:必需的问题[]参数“allQuestions”不存在]

我不明白为什么会出现这个错误。我已经在客户端的HttpParams类实例中传递了“allQuestions”参数

如何解决此问题?

像这样做吧

this.http
      .post("/questions/import", undefined, { headers: headers, params })
顺便说一下,如果您在spring boot中发送json正文,您应该使用@RequestBody而不是@RequestParam,因为@RequestParam用于queryparameters。并发送如下请求:-

this.http
          .post("/questions/import", this.dataSource, { headers: headers})
喜欢吗

this.http
      .post("/questions/import", undefined, { headers: headers, params })
顺便说一下,如果您在spring boot中发送json正文,您应该使用@RequestBody而不是@RequestParam,因为@RequestParam用于queryparameters。并发送如下请求:-

this.http
          .post("/questions/import", this.dataSource, { headers: headers})

从post请求发送时,主体必须为字符串类型

    const headers = new HttpHeaders().set(
      "Content-Type",
      "application/json; charset=utf-8"
    );

    let params: string = JSON.stringify(this.dataSource);

    this.http
      .post("/questions/import", params, { headers: headers })
      .subscribe(
        (data) => {
          this.dialog.close(true);
        },
        (error) => this.displayErrorMessage(error)
      );
第二个问题是,对于spring中的post请求,您希望@RequestBody而不是@RequestParam,如下所示:

 @RequestMapping(path = "/questions/import", method = RequestMethod.POST,
        consumes = "application/json")
 public ResponseEntity<String> importQuestions(@RequestBody Question[] allQuestions) { ...  }
@RequestMapping(path=“/questions/import”,method=RequestMethod.POST,
consumes=“application/json”)
公共响应重要问题(@RequestBody Question[]allQuestions){…}

从post请求发送时,正文必须为字符串类型

    const headers = new HttpHeaders().set(
      "Content-Type",
      "application/json; charset=utf-8"
    );

    let params: string = JSON.stringify(this.dataSource);

    this.http
      .post("/questions/import", params, { headers: headers })
      .subscribe(
        (data) => {
          this.dialog.close(true);
        },
        (error) => this.displayErrorMessage(error)
      );
第二个问题是,对于spring中的post请求,您希望@RequestBody而不是@RequestParam,如下所示:

 @RequestMapping(path = "/questions/import", method = RequestMethod.POST,
        consumes = "application/json")
 public ResponseEntity<String> importQuestions(@RequestBody Question[] allQuestions) { ...  }
@RequestMapping(path=“/questions/import”,method=RequestMethod.POST,
consumes=“application/json”)
公共响应重要问题(@RequestBody Question[]allQuestions){…}