KB:MultipartのPOSTで `Required request part 'file' is not present`

f:id:vemi:20201213192936p:plain

BootStrapVue で b-form-file 部品を使って
Multipart のアップロードを実装していたところ、
Required request part 'file' is not present という
SpringBoot がわのエラーにハマりました。。

MultipartResolver の設定をして見ろだとか、
#configureMessageConverters を拡張して見ろだとか、
色々試してもウマくいかず。

結論から言うと、 Form Data に渡すオブジェクトが間違っていました。。

修正前の実装

■Nuxt.js

    callUploadApi (uploadingFiles) {
      const formData = new FormData()
      formData.append('files', uploadingFiles)
      axios.post(
        `/mapi/commons/upload`,
        formData).then((resp) => {

■SpringBoot

@RestController
public class UploadController {
    @RequestMapping(path = "commons/upload")
    public ResponseEntity<ApiResponse<FileUploadResult>> index(
            @RequestParam(name = "files") @Valid @NotNull @NotBlank MultipartFile[] multipartFiles) {

Debug log

trace: "org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present
↵ at org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver.resolveArgument(RequestPartMethodArgumentResolver.java:161)
↵ at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
↵ at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:170)
↵ at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
↵ at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
↵ at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893)
↵ at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:807)
↵ at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
↵ at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1061)
↵ at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:961)
↵ at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
↵ at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
↵ at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
↵ at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
↵ at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:327)
↵ at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)
↵ at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
↵ at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
↵ at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:126)
↵ at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:81)
↵ at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
↵ at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:105)
↵ at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
↵ at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:149)
↵ at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
↵ at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
↵ at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
↵ at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:103)
↵ at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:89)
↵ at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
↵ at org.springframework.security.web.header.HeaderWriterFilter.doHeadersAfter(HeaderWriterFilter.java:90)
↵ at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:75)
↵ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
↵ at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
↵ at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:110)
↵ at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:80)
↵ at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
↵ at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:55)
↵ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
↵ at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
↵ at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:211)
↵ at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:183)
↵ at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
↵ at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
↵ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
↵ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:141)
↵ at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:82)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:93)
↵ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
↵ at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
↵ at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
↵ at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
↵ at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
↵ at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
↵ at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
↵ at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
↵ at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
↵ at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
↵ at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
↵ at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
↵ at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
↵ at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
↵ at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
↵ at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
↵ at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
↵ at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
↵ at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
↵ at java.base/java.lang.Thread.run(Thread.java:832)
↵"

リクエストがマルチパート / フォームデータリクエストではない、リクエストにパーツが存在しない、または Web アプリケーションがマルチパートリクエストを処理するために正しく設定されていないなどの理由による可能性があります。

spring.pleiades.io

Form Data は object で送信されていました。
f:id:vemi:20201213190624p:plain

ためしに、Blob に変換したところ、受付はできたのですが、
ファイルの中身は壊れていました。

修正後

配列としての複数ファイルの Multipart での送り方が分からなかったので
一旦単一ファイルを受け渡すように修正して回避しました。。

(file1, file2,... なんて書き方は絶対にしたくない…)

■Nuxt.js

    callUploadApi (uploadingFiles) {
      const formData = new FormData()
      formData.append('file', uploadingFiles[0])
      axios.post(
        `/mapi/commons/upload`,
        formData).then((resp) => {

■SpringBoot

@RestController
public class UploadController {
    @RequestMapping(path = "commons/upload")
    public ResponseEntity<ApiResponse<FileUploadResult>> index(
            @RequestParam(name = "file") @Valid @NotNull @NotBlank MultipartFile multipartFile) {

めでたしめでたし めでたくない。。