카테고리 없음

이 mutlipart 파일 업로드를 Spring Boot 2.4로 마이그레이션하는 방법은 무엇입니까?

기록만이살길 2021. 2. 23. 19:05
반응형

이 mutlipart 파일 업로드를 Spring Boot 2.4로 마이그레이션하는 방법은 무엇입니까?

1. 질문(문제점):

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함께 a 발생시키고 HTTP 응답 코드 400을 발생시킵니다.

마이그레이션 가이드 및이 버전 변경에 대해 처리 된 문제 목록에서 아무것도 찾지 못했습니다.

file컨트롤러와 요청 모두에서 이름을 바꾸는 것은 도움이되지 않습니다. []2.3에서 작동하는 코드에 왜 추가했는지 기억이 나지 않지만 작동하도록해야한다고 생각합니다.

.NET과 함께 maven 부모 메커니즘을 통해 Spring Boot를 사용하고 spring-boot-starter-parent:2.4.1있습니다.

2. 해결방안:

이것은 Spring 에서 오는 Spring Boot의 알려진 문제입니다 . Spring Boot 2.4.2에서 수정되었습니다. 연결된 문제에는 2.4.1 : Create MockMultipartFilewith MockMultipartFile( String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable byte[] content)( originalFilename사건 사양)를 사용하는 경우에 대비하여 성공적으로 테스트 된 해결 방법이 포함되어 있습니다 .

65745145
반응형