【问题标题】:Spring boot http POST @RequestParam multiple parametersSpring boot http POST @RequestParam 多参数
【发布时间】:2018-07-11 12:29:47
【问题描述】:

我有一个弹簧靴admin server 和一个angular-client 前部。我正在尝试使用 HTTPClient 将一些数据从我的前端发送到我的服务器,但不知何故,我在请求期间收到以下错误,但首先是我的代码:

angular-client 中的 POST 请求:

  runBatch(id: number, stateUpd: string): Observable<HttpEvent<{}>> {
    const req = new HttpRequest('POST', '/update_state', {id, stateUpd}, {
      reportProgress: true,
      responseType: 'text'
    });
    return this.http.request(req);
  }

angular-client 中的控制器:

changeBatchState(state: string): void {
        if(this.selection.selected.length >= 1){
            this.selection.selected.forEach(batchInstance =>{
                if(batchInstance.batchState == 'CRASHED' || 'KILLED' || 'SUBMITTED'){
                    console.log(batchInstance.id + " setting to RUNNABLE...");
                    this.dataTableService.runBatch(batchInstance.id, state).subscribe(event => {
                        if(event.type === HttpEventType.UploadProgress) {
                         console.log('POST /update_state sending...');   
                        }
                        else if(event instanceof HttpResponse) {
                         console.log('Request completed !');   
                        }
                    });
                }
                else {
                    console.log(batchInstance.id + ' can not set to RUNNABLE');
                }
            });
        }
    }

admin server 中的控制器:

    @PostMapping("/update_state")
    public ResponseEntity<String> batchChangeState(@RequestParam("id") int id, @RequestParam("stateUpd") String stateUpd) {
        try {
            log.i("INSIDE CONTROLLER");
            log.i("BATCH INSTANCE ID : " + id);
            log.i("UPDATE REQUESTED : " + stateUpd);

        return ResponseEntity.status(HttpStatus.OK).body("Batch instance: " + id + " updated");
        } catch (Exception e) {

        return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body("Fail to update batch instance " + id);
        }
    }

这里是我在请求期间得到的错误:

ERROR 
Object { headers: {…}, status: 400, statusText: "OK", url: "http://localhost:8870/update_state", ok: false, name: "HttpErrorResponse", message: "Http failure response for http://localhost:8870/update_state: 400 OK", error: "{\"timestamp\":1517473012190,\"status\":400,\"error\":\"Bad Request\",\"exception\":\"org.springframework.web.bind.MissingServletRequestParameterException\",\"message\":\"Required int parameter 'id' is not present\",\"path\":\"/update_state\"}" }

我不明白它来自哪里,因为我在我的 POST 请求中正确发送了 id,有什么想法吗?

【问题讨论】:

  • 您可以在开发者控制台中查看您的发帖请求吗?您可以尝试发送这样的参数: new HttpRequest('POST', `/update_state?id=${id}&amp;stateUpd=${stateUpd}`,...
  • 我正在尝试这样的错误:Error parsing HTTP request header 现在正在调试以向您提供请求表..
  • 它不会用 ${id} 和 ${stateUpd} 的值替换它们

标签: angular http typescript spring-boot httpclient


【解决方案1】:
const req = new HttpRequest('POST', '/update_state', {id, stateUpd}, {

将创建一个请求,其中 {id, stateUpd} 在正文中,而不是在 queryParams 中。

你应该这样做

// change : Body = null, and data are in options.params    
runBatch(id: number, stateUpd: string): Observable<HttpEvent<{}>> {
        const req = new HttpRequest('POST', '/update_state', null, {
          reportProgress: true,
          responseType: 'text',
          params: new HttpParams().set('id', id.toString()).set('stateUpd', stateUpd);
        });
        return this.http.request(req);
      }

【讨论】:

  • params 是 HttpParams 类型,我在这样做时遇到类型错误。
  • 你的角度版本是多少?添加HttpParams的好方法见stackoverflow.com/questions/45210406/…
  • 参数:{ id: id.toString(), stateUpd: stateUpd }
  • 应该是这样但是id: id.toString() 还是有类型错误,原因不明..
  • 属性“参数”的类型不兼容。键入'{ id:字符串; stateUpd:字符串; }' 不可分配给类型 'HttpParams'。对象字面量只能指定已知属性,并且 'id' 不存在于类型 'HttpParams' 中。 (方法) Number.toString(radix?: number): string
猜你喜欢
  • 2013-01-17
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-06
  • 2017-11-25
  • 2018-11-05
相关资源
最近更新 更多