我将 spring-boot 2.0.3.RELEASE 与 junit5 一起使用。我刚刚尝试使用 mockMvc 测试多部分请求。
MockMultipartFile file = new MockMultipartFile("file", "contract.pdf", MediaType.APPLICATION_PDF_VALUE, "<<pdf data>>".getBytes(StandardCharsets.UTF_8));
MockMultipartFile metadata = new MockMultipartFile("metadata", "", MediaType.APPLICATION_JSON_VALUE, objectMapper.writeValueAsString(metadata).getBytes(StandardCharsets.UTF_8));
this.mockMvc.perform(multipart("/documents")
.file(file)
.file(metadata))
.andExpect(status().isNoContent());
但它不适用于部分元数据(json)。
我总是遇到异常
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: originalFilename is required.
怎么了?
编辑:
我的实现是在 spring 集成 dsl 中。我认为该异常是在org.springframework.integration.http.multipart.UploadedMultipartFile
. 如何使用 mockMvc 测试多部分请求?
@Bean
public IntegrationFlow multipartForm() {
return IntegrationFlows.from(Http.inboundChannelAdapter("/documents")
.statusCodeExpression(HttpStatus.NO_CONTENT.toString())
.requestMapping(m -> m
.methods(HttpMethod.POST)
.consumes(MediaType.MULTIPART_FORM_DATA_VALUE)
))
.handle(new MultipartReceiver())
.get();
}
我的演示项目在 github https://github.com/bulalak/demo-spring-integration-http