3

I have a Java 11 application using spring boot.

I have this request in my request controller :

@RestController
public class ImportController {
     private static final Logger LOGGER = LoggerFactory.getLogger(ImportController.class);

     @PostMapping("/import")
     public ResponseEntity<List<CompareAnalysis>> importXML(@RequestParam("files") List<MultipartFile> files) {
        LOGGER.debug("issue here" + files.size());
...
     }
}

After generating my war I put it in my Tomcat Vanilla 9.0.45.

When I try to call my application like this: curl -X POST -F 'files=@toto.pdf' http://localhost:8080/import/, I always have in my logs :

17:22:33.461 [http-nio-8080-exec-3] DEBUG my.app.controler.ImportController - issue here 0

I don't know where I lose my multipart file...

Remi Colas
  • 41
  • 4

1 Answers1

1

I finally found the issue: I miss configuration in my Initializer.

Like I saw on this site https://www.dev2qa.com/spring-mvc-file-upload-unable-to-process-parts-as-no-multi-part-configuration-has-been-provided/ I add the multipart element in my Initializer

public class AppInitializer implements WebApplicationInitializer {

   @Override
   public void onStartup(ServletContext container) throws ServletException {
       ...

       MultipartConfigElement multipartConfig = new MultipartConfigElement("/tmp");
       dispatcher.setMultipartConfig(multipartConfig);

       dispatcher.setLoadOnStartup(1);

       dispatcher.addMapping("*.html");

       FilterRegistration.Dynamic multipartFilter = container.addFilter("multipartFilter", MultipartFilter.class);
       multipartFilter.addMappingForUrlPatterns(null, true, "/*");

   }
}

It resolves everything for me.

Remi Colas
  • 41
  • 4
  • 1
    Why do you have an initializer in the first place? If you are using Spring bOot you shouldn't need this. – M. Deinum May 27 '21 at 09:51
  • Without my initializer all my calls return a 404. I think it's because my tomcat run my back and my angular front. – Remi Colas May 27 '21 at 12:30
  • If you need that initializer you are doing things wrong with Spring Boot, whichmeans you are working around things. Looks to me as if you haven't read the section on how to create a war with Spring Boot for deployment (hint it is more then switching packaging to war). – M. Deinum May 27 '21 at 12:32