【问题标题】:How to migrate this mutlipart file upload to Spring Boot 2.4?如何将此分段文件上传迁移到 Spring Boot 2.4?
【发布时间】:2021-01-16 00:07:54
【问题描述】:

在 Spring Boot 2.3 中,我使用了以下 Kotlin 代码

val mvcResultImage = this.mockMvc!!.perform(MockMvcRequestBuilders.multipart("/somepath)
        .file("files[]", imageFile.getBytes())
        .characterEncoding("UTF-8"))
        .andReturn()

在带有函数的控制器的集成测试中

@PostMapping(path = ["/somepath"],
    consumes = [MediaType.MULTIPART_FORM_DATA_VALUE],
    produces = [MediaType.APPLICATION_JSON_VALUE])
@ResponseBody
fun createFromBytes(@RequestParam("files[]") file: MultipartFile): ResponseEntity<Any> {
   ...
}

在 2.3 中,我能够在控制器函数中处理请求,而在 2.4 中,控制器函数引发 org.springframework.web.multipart.support.MissingServletRequestPartException 和消息 Required request part 'files[]' is not present 并导致 HTTP 响应代码 400。

我在迁移指南和此版本更改的已处理问题列表中找不到任何内容。

在控制器和请求中重命名为 file 没有帮助,我不记得为什么我在使用 2.3 的代码中添加了 [],但我认为有必要让它工作。

我通过带有spring-boot-starter-parent:2.4.1 的maven 父机制使用Spring Boot。

【问题讨论】:

    标签: spring spring-boot kotlin multipartform-data http-request-parameters


    【解决方案1】:

    这是来自 Spring 的 a known issues in Spring Boot。它已在 Spring Boot 2.4.2 中修复。链接的问题包含一个成功测试的解决方法,以防您遇到 2.4.1:使用MockMultipartFile( String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable byte[] content) 创建MockMultipartFileoriginalFilename 的规范很重要)。

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 1970-01-01
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      相关资源
      最近更新 更多