【发布时间】:2021-04-09 15:43:14
【问题描述】:
我正在尝试使用多部分 POST 请求向我的 Spring Boot 后端发送一个文件和两个 json 对象,但我总是收到 415 http 响应。以下是我已经尝试过的事情的清单:
- 将每个对象作为
Blob文件发送,内容类型为application/json建议here - 按照建议here 将每个对象作为
String发送 - 按照建议here在ajax请求中添加
contentType: false和processData: false - 在 Spring Boot 控制器中使用
@RequestParam而不是@RequestPart
我错过了什么?
这是请求:
const data = new FormData();
data.append('file', new Blob([file], {type: 'multipart/form-data'}));
data.append('entity1-info', new Blob([JSON.stringify(entity1Object)], {type: 'application/json'}));
data.append('entity2-info', new Blob([JSON.stringify(entity2Object)], {type: 'application/json'}));
return axios({
method: 'post',
url: url,
headers: {'Authorization': `Bearer ${idToken}`},
data: data,
contentType: false,
processData: false
});
这是我在 Spring Boot 中的控制器:
@PostMapping("/upload")
public ResponseEntity<String> uploadFile(@NotEmpty @RequestPart("file") MultipartFile multipartFile, @NotNull @RequestPart("entity1-info") Entity1 entity1, @NotNull @RequestPart("entity2-info") Entity2 entity2, HttpServletRequest request) {
log.debug(request);
...
return ResponseEntity.ok("ok");
}
【问题讨论】:
标签: javascript java json spring-boot