【问题标题】:Passing a complex FormData from angular 5 to Java Spring 3将复杂的 FormData 从 Angular 5 传递到 Java Spring 3
【发布时间】:2018-07-20 12:58:50
【问题描述】:

我正在尝试向 FormData 中插入一个数组数组和一个字符串,但是 java 似乎没有收到它,我的 Java 服务器中没有日志错误,但是我的 JavaScript 控制台中有 500 内部服务器错误。

这是我的控制器的代码:

 @RequestMapping(value = "/getReporting", method = RequestMethod.POST)
  @ResponseBody
  public void  getReporting(@RequestParam RecommendationForm form, @RequestParam String type, HttpServletResponse response) throws ApcException {

    System.out.println("prova");
    Map.Entry<String, byte[]> result =  this.reportingService.getReporting(form,type);

    try {
      response.setHeader(//
        "Content-Disposition",//
        "attachment; filename=" +"bobo.xlsx");
      response.setContentType("Application/x");
      response.getOutputStream().write(result.getValue());
      response.flushBuffer();
    } catch (IOException e) {

      e.printStackTrace();
    }

  }
}

这是我在 Angular 中的服务:

 public getExcel(form: FormData): Observable<HttpResponse<Blob>> {

    return this.http.post('/SV-AUD/api/reporting/getReporting', form, {observe: 'response', responseType: 'blob'});
  }

以及我在 formData 中附加信息的组件:

form: FormGroup = this._fb.group(
    {
      hello1: [],
      hello2: [],
      hello3: [],
      hello4: [],
      hello5: [],
      hello6: [],
      hello7: [],
      hello8: [],
      hello9: [],
    }
  );

exportExcel() {

    const formData: FormData = new FormData();
    formData.append('form', this.form.getRawValue());

    if (this.detailedType) {
      formData.append('type', 'detailed');
    } else {
      formData.append('type', 'list');
    }
    this.reportingService.getExcel(formData).subscribe(data => {
      const ctHeader = data.headers.get('content-disposition');
      if (ctHeader) {
        const filename = ctHeader.split('=')[1];
        saveAs(data.body, filename);
      }
    });

  }

【问题讨论】:

  • 您检查过日志中的确切错误吗?
  • 我的服务器日志中没有错误,在我的 JavaScript 中有一个 HttpErrorResponse 状态 500

标签: javascript java spring angular


【解决方案1】:

您所描述的行为表明 Spring 无法将您的 @RequestParam 方法的 @RequestParam 参数绑定到传入请求。

这意味着您从 Angular 端发布的数据与 Spring 端的预期不匹配。

除非是拼写错误,否则我猜问题出在组件源代码中的这一行,它什么也没做(应该是由于括号不匹配而导致的语法错误):

  (this.form.getRawValue()));

我猜应该是:

 formData.append('form', (this.form.getRawValue()));

【讨论】:

  • 请原谅,正确的行是 formData.append('form', this.form.getRawValue());我不知道为什么它没有正确复制我会尝试你的解决方案
  • 我试过了,但没有解决问题,它仍然给我 500 内部服务器错误,服务器端没有任何日志
猜你喜欢
  • 2014-06-17
  • 1970-01-01
  • 2020-02-05
  • 1970-01-01
  • 2019-12-21
  • 2017-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多