title: Spring boot upload form data and file
body: <p>I am making a spring boot REST application. I am trying to make a multipart form upload controller which will handle a form data and a file upload together. This is my controller code at the moment :</p>
  
  <pre><code>    @RequestMapping(value = "", method = RequestMethod.POST, headers="Content-Type=multipart/form-data")
      @PreAuthorize("hasRole('ROLE_MODERATOR')")
      @ResponseStatus(HttpStatus.CREATED)
      public void createNewObjectWithImage(
              /*@RequestParam(value="file", required=true) MultipartFile file,
              @RequestParam(value="param_name_1", required=true) final String param_name_1,
              @RequestParam(value="param_name_2", required=true) final String param_name_2,
              @RequestParam(value="param_name_3", required=true) final String param_name_3,
              @RequestParam(value="param_name_4", required=true) final String param_name_4,
              @RequestParam(value="param_name_5", required=true) final String param_name_5*/
              @ModelAttribute ModelDTO model,
              BindingResult result) throws MyRestPreconditionsException {
  
          //ModelDTO model = new ModelDTO(param_name_1, param_name_2, param_name_3, param_name_4, param_name_5);
  
          modelValidator.validate(model, result);
          if(result.hasErrors()){
              MyRestPreconditionsException ex = new MyRestPreconditionsException(
                      "Model creation error",
                      "Some of the elements in the request are missing or invalid");
              ex.getErrors().addAll(
                      result.getFieldErrors().stream().map(f -&gt; f.getField()+" - "+f.getDefaultMessage()).collect(Collectors.toList()));
              throw ex;
          }
  // at the moment, model has a MultipartFile property
          //model.setImage(file);
          modelServiceImpl.addNew(model);
      }
  </code></pre>
  
  <p>I have tried both with the @ModelAttribute annotation and sending request parameters, but both of these methods have failed.</p>
  
  <p>This is the request i am sending :</p>
  
  <pre><code>---------------------------acebdf13572468
  Content-Disposition: form-data; name="file"; filename="mint.jpg"
  Content-Type: image/jpeg
  
  &lt;@INCLUDE *C:\Users\Lazaruss\Desktop\mint.jpg*@&gt;
  ---------------------------acebdf13572468
  Content-Disposition: form-data; name=”param_name_1”
  
  string_value_1
  ---------------------------acebdf13572468
  Content-Disposition: form-data; name=”param_name_2”
  
  string_value_2
  ---------------------------acebdf13572468
  Content-Disposition: form-data; name=”param_name_3”
  
  string_value_3
  ---------------------------acebdf13572468
  Content-Disposition: form-data; name=”param_name_4”
  
  string_value_4
  ---------------------------acebdf13572468
  Content-Disposition: form-data; name=”param_name_5”
  
  string_value_5
  ---------------------------acebdf13572468--
  </code></pre>
  
  <p>My application is stateless, and uses spring security with authorities.
  In my security package, i have included the AbstractSecurityWebApplicationInitializer class</p>
  
  <pre><code>public class SecurityApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
  
      @Override
      protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
              insertFilters(servletContext, new MultipartFilter());
          }
      }
  </code></pre>
  
  <p>I also use a StandardServletMultipartResolver in my @Configuration class</p>
  
  <p>And in my WebInitializer, i add this code :</p>
  
  <pre><code>MultipartConfigElement multipartConfigElement = new MultipartConfigElement("/tmp", 
                  3 * 1024 * 1024, 6 * 1024 * 1024, 1 * 512 * 1024);        
  apiSR.setMultipartConfig(multipartConfigElement);
  </code></pre>
  
  <p>When i try to use the controller with the commented code (@RequestParams annotations), i get a 404 not found error. 
  And when i try to use the controller with the @ModuleAttribute annotation, the model object is empty.</p>