将 multipartFile 从 Angular 发送到 Java
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能使用
@RequestBody
您必须使用@RequestPart
如果您需要随文件发送 DTO,请
formData.append('dto', JSON.stringify(data));
在 Java 部分添加另一个
@RequestPart(value = "dto") String stringDTO)
手动调用对象映射器:
YourDtoClass dto = objectMapper.readValue(stringDTO, YourDtoClass.class);
objectMapper
来自包com.fasterxml.jackson.databind
You can not use a
@RequestBody
you have to use a@RequestPart
And if you need to send a DTO with your file,
formData.append('dto', JSON.stringify(data));
on the Java part add another
@RequestPart(value = "dto") String stringDTO)
call manually your object mapper :
YourDtoClass dto = objectMapper.readValue(stringDTO, YourDtoClass.class);
objectMapper
from packagecom.fasterxml.jackson.databind