[英]How to fix "Required request part is not present" in a Spring Boot multipart upload
While my question is similar to this one , I am not using the MockMultipartFile
.虽然我的问题与此类似,但我没有使用MockMultipartFile
。 Here my use case.这是我的用例。
In a Spring Boot 2.4.5 application, I have written a unit test which executes code, that should send a multipart/form-data
request.在 Spring Boot 2.4.5 应用程序中,我编写了一个执行代码的单元测试,它应该发送一个multipart/form-data
请求。 To be able to test the receiving end from the test, I have started a Controller
as a receiver for the multipart/form-data
call.为了能够从测试中测试接收端,我启动了一个Controller
作为multipart/form-data
调用的接收器。 This is the controller (it is currently not doing anything with the parameters).这是 controller(它目前没有对参数做任何事情)。
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/v1/api")
public class VideoServiceStub {
@PostMapping(value ="/videoservice/videos", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> upload(@RequestPart("name") MultipartFile name, @RequestPart("file") MultipartFile file) {
return ResponseEntity.status(HttpStatus.OK).body("it works");
}
}
What makes my use case special, is that the POST shall be made from an in-memory byte array or InputStream .我的用例的特别之处在于 POST 应由内存中的字节数组或InputStream制成。 To give some context, my code will later download a file from a URL and make this multipart/form-data
to relay the data and send it someplace else.为了提供一些上下文,我的代码稍后将从 URL 下载一个文件,并使这个multipart/form-data
中继数据并将其发送到其他地方。 The unit test will trigger the following code to execute, which uses Spring Rest Template.单元测试将触发以下代码执行,该代码使用 Spring Rest 模板。
protected void sendToVideoService(String name, byte[] content) {
var headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
var body = new LinkedMultiValueMap<>();
body.add("name", name);
body.add("file", new ByteArrayResource(content));
var url = this.uploadUrl(); // the test will return the correct localhost URL here
var requestEntity = new HttpEntity<>(body, headers);
restTemplate.postForEntity(url, requestEntity, String.class);
}
It fails with a HttpClientErrorException$BadRequest
and I can see this in the stdout logs:它以HttpClientErrorException$BadRequest
失败,我可以在标准输出日志中看到:
2021-06-01 07:23:33.027 WARN 27432 --- [o-auto-1-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver :
Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'name' is not present]
To verify, that the problem was on the receiving end, I tested a version using the newer java.net.http.HttpClient
together with the great methanol library.为了验证问题出在接收端,我使用较新的java.net.http.HttpClient
和出色的甲醇库测试了一个版本。
protected void sendToVideoServiceUsingJava11(String name, byte[] content) {
var url = this.uploadUrl(); // the test will return the correct localhost URL here
var methanol = Methanol.create();
var multipartBody = MultipartBodyPublisher.newBuilder()
.textPart("name", name)
.formPart("file", HttpRequest.BodyPublishers.ofByteArray(content))
.build();
var request = MutableRequest.POST(url, multipartBody);
methanol.send(request, HttpResponse.BodyHandlers.ofString());
}
I can see the exact same warning in stdout:我可以在标准输出中看到完全相同的警告:
2021-06-01 07:22:35.086 WARN 23840 --- [o-auto-1-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver :
Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'name' is not present]
400
Finally, I have this in my application.yaml
:最后,我的application.yaml
中有这个:
spring:
servlet:
multipart:
max-file-size: 750MB
max-request-size: 750MB
server:
tomcat:
max-swallow-size: 786432000
max-http-form-post-size: 786432000
logging:
level:
org:
springframework:
web:
filter:
CommonsRequestLoggingFilter: DEBUG
I managed to solve the issue.我设法解决了这个问题。 In the VideoServiceStub
the name
param must be a String
instead of a MultipartFile
.在VideoServiceStub
中, name
参数必须是String
而不是MultipartFile
。
@PostMapping(value ="/videoservice/videos", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> upload(@RequestPart("name") String name, @RequestPart("file") MultipartFile file) {
return ResponseEntity.status(HttpStatus.OK).body("it works");
}
Furthermore in sendToVideoServiceUsingJava11
, it doesn't work without a filename
:此外,在sendToVideoServiceUsingJava11
中,如果没有filename
,它就无法工作:
.formPart("file", "filename.mp4", HttpRequest.BodyPublishers.ofByteArray(content))
I never got the Spring REST Template example to work.我从来没有让 Spring REST 模板示例工作。 There is no way to pass a filename along when doing: body.add("file", new ByteArrayResource(content))
.这样做时无法传递文件名: body.add("file", new ByteArrayResource(content))
。 Maybe someone has an idea?也许有人有想法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.