将 multipartFile 从 Angular 发送到 Java

发布于 2025-01-14 16:06:42 字数 2136 浏览 1 评论 0原文

我正在使用 Java 创建 API,但遇到文件问题。

这是我的 API 定义:

@RequestMapping(value="/createUser", method = RequestMethod.POST)
ResponseEntity<?> fromFile(@RequestBody FileRequest FileRequest);

这个 FileRequest 输入 Bean 只有一项(当我解决问题时),但将来它将有更多项,例如 userName 或 userAge 等其他表单数据。

public class FromFileRequest {

  @NotEmpty(message = "Value for input 'file' can't be null" )
  private MultipartFile file;

  public MultipartFile getFile() {
    return file;
  }

  public void setFile(MultipartFile file) {
    this.file = file;
  }
}

在角度方面:

load(file: File): Observable<any> {
    let formData: FormData = new FormData();    
    formData.append('file', file);
    return this.apiService.post('/api/createFile', formData);
}

我在 Java 方面收到错误,

09:31:51,010 INFO [stdout] (http-127.0.0.1:8080-4) org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.springframework.web.multipart.MultipartFile]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.web.multipart.MultipartFile` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
09:31:51,010 INFO [stdout] (http-127.0.0.1:8080-4) at [Source: (PushbackInputStream); line: 1, column: 9] (through reference chain: com.mypackage.api.dto.FileRequest["file"])
09:31:51,010 INFO [stdout] (http-127.0.0.1:8080-4) at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:242) ~[spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
09:31:51,010 INFO [stdout] (http-127.0.0.1:8080-4) at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:227) ~[spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]

我真的不知道问题是在角度还是 Java 方面,因为如果我只使用 MultipartFile 类型作为 API 中的输入(并正确修改角度),它就可以工作。

I'm creating an API with Java, and I'm facing a problem with files.

This is my API definition:

@RequestMapping(value="/createUser", method = RequestMethod.POST)
ResponseEntity<?> fromFile(@RequestBody FileRequest FileRequest);

This FileRequest input Bean only have one item (while I solve the problem) but in future it will have more items such other form data as userName or userAge.

public class FromFileRequest {

  @NotEmpty(message = "Value for input 'file' can't be null" )
  private MultipartFile file;

  public MultipartFile getFile() {
    return file;
  }

  public void setFile(MultipartFile file) {
    this.file = file;
  }
}

In the angular side:

load(file: File): Observable<any> {
    let formData: FormData = new FormData();    
    formData.append('file', file);
    return this.apiService.post('/api/createFile', formData);
}

I receive an error in Java side

09:31:51,010 INFO [stdout] (http-127.0.0.1:8080-4) org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.springframework.web.multipart.MultipartFile]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.web.multipart.MultipartFile` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
09:31:51,010 INFO [stdout] (http-127.0.0.1:8080-4) at [Source: (PushbackInputStream); line: 1, column: 9] (through reference chain: com.mypackage.api.dto.FileRequest["file"])
09:31:51,010 INFO [stdout] (http-127.0.0.1:8080-4) at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:242) ~[spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]
09:31:51,010 INFO [stdout] (http-127.0.0.1:8080-4) at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:227) ~[spring-web-5.1.2.RELEASE.jar:5.1.2.RELEASE]

I really don't know if the problem is in angular or Java side, because if I just use the MultipartFile type as the input in my API (and modify angular properly) it works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷路的信 2025-01-21 16:06:42

您不能使用 @RequestBody 您必须使用 @RequestPart

@RequestMapping(value="/createUser", method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
ResponseEntity<?> fromFile(@RequestPart(value = "file") MultipartFile file)

如果您需要随文件发送 DTO,请

  1. 在角度一侧手动对其进行字符串化:

formData.append('dto', JSON.stringify(data));

  1. 在 Java 部分添加另一个
    @RequestPart(value = "dto") String stringDTO)

  2. 手动调用对象映射器:

YourDtoClass dto = objectMapper.readValue(stringDTO, YourDtoClass.class);

objectMapper 来自包 com.fasterxml.jackson.databind

You can not use a @RequestBody you have to use a @RequestPart

@RequestMapping(value="/createUser", method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
ResponseEntity<?> fromFile(@RequestPart(value = "file") MultipartFile file)

And if you need to send a DTO with your file,

  1. stringify it manually on the angular side :

formData.append('dto', JSON.stringify(data));

  1. on the Java part add another
    @RequestPart(value = "dto") String stringDTO)

  2. call manually your object mapper :

YourDtoClass dto = objectMapper.readValue(stringDTO, YourDtoClass.class);

objectMapper from package com.fasterxml.jackson.databind

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文