title: File upload Restful API: Spring-boot
body: <p>I'm trying to make image uploading but I get this error, and I don't know why. I've already tried many things but I'm still getting errors.</p>
  
  <p>Firstly, this:</p>
  
  <pre><code>{
  "timestamp": 1454645660390
  "status": 405
  "error": "Method Not Allowed"
  "exception": "org.springframework.web.HttpRequestMethodNotSupportedException"
  "message": "Request method 'POST' not supported"
  "path": "/usuarios/update"
  }
  </code></pre>
  
  <p>This is my controller:
  Note: returns <code>null</code> for testing.</p>
  
  <pre><code>@RequestMapping(value = "/update", method = RequestMethod.POST, headers = MediaType.MULTIPART_FORM_DATA_VALUE)
  public ResponseEntity&lt;Usuarios&gt; updateUsuario(
          OAuth2Authentication authentication,
          HttpServletRequest req,
          @RequestBody Usuarios usuarios,
          @RequestParam("file") MultipartFile file) {
      req.getHeaderNames();
      file.getName();
      return null;
  }
  </code></pre>
  
  <p>And this is my <code>MultipartResolver</code>:</p>
  
  <pre><code>@Bean
  public MultipartResolver multipartResolver() {
      CommonsMultipartResolver resolver = new CommonsMultipartResolver();
      resolver.setMaxUploadSize(1000000);
      return resolver;
  }
  </code></pre>
  
  <p>Any suggestions what I'm doing wrong? Thank you so much!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've updated my <code>@Bean</code>:</p>
  
  <pre><code>@Configuration
  @ComponentScan
  @EnableAutoConfiguration
  public class TierraApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(TierraApplication.class, args);
      }
  
      @Bean
      public MultipartConfigElement multipartConfigElement() {
          return new MultipartConfigElement("");
      }
  
      @Bean
      public MultipartResolver multipartResolver() {
          CommonsMultipartResolver resolver = new CommonsMultipartResolver();
          resolver.setMaxUploadSize(1000000);
          return resolver;
      }
  }
  </code></pre>
  
  <p>and the method on my <code>@RestController</code>:</p>
  
  <pre><code>@RestController
  @RequestMapping("/usuarios")
  public class UsuariosController implements Serializable {
  @RequestMapping(value = "/update", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
      public ResponseEntity&lt;Usuarios&gt; updateUsuario(
              @RequestBody Usuarios usuarios,
              @RequestParam("file") MultipartFile file) {
          file.getName();
          return null;
      }
  }
  </code></pre>
  
  <p>but now i'm getting this error:</p>
  
  <pre><code>    {
  "timestamp": 1454683574928
  "status": 415
  "error": "Unsupported Media Type"
  "exception": "org.springframework.web.HttpMediaTypeNotSupportedException"
  "message": "Content type 'multipart/form-data;boundary=----WebKitFormBoundary6GTTqiBmiacyW0xb;charset=UTF-8' not supported"
  "path": "/usuarios/update"
  }
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>Ok, I've deleted the <code>@Bean</code> of multipartResolver and <code>@RequestBody</code> and all works fine.</p>
  
  <pre><code>@RequestMapping(value = "/update", method = RequestMethod.POST)
  public ResponseEntity&lt;?&gt; updateUsuario(@RequestParam("file") MultipartFile file,
                     OAuth2Authentication authentication,
          HttpServletRequest req) {       
      try {
  
      } catch (Exception e) {
          return new ResponseEntity&lt;&gt;(HttpStatus.BAD_REQUEST);
      }
  
      return new ResponseEntity&lt;&gt;(name, HttpStatus.OK);
  }
  </code></pre>
  
  <p>But now I can't reach my body in the request. If I put it got again all the same errors. So how can I pass or reach the body with a JSON like this?</p>
  
  <pre><code>{
  "idUsuario": 1,
  "roles":{"idRol": 1, "nombreRol": "ADMINISTRADOR", "fechaCreacion": "2016-01-31", "fechaModificacion": null,…},
  "nombre": "User",
  "apellido": "Test",
  "fechaNacimiento": "1992-04-04",
  "dni": 38078020,
  "email": "test@hotmail.com",
  "telefono": 155797919,
  "domicilio": "test 972",
  "provincia": "San Salvador de Jujuy",
  "username": "tester",
  "imagen": null,
  "estado": true,
  "fechaCreacion": "2016-02-03",
  "fechaModificacion": null,
  "idUsuarioCreacion": 1,
  "idUsuarioModificacion": 0,
  "passwordUsuario": "$2a$10$SGueYkRnMkL43Ns1nmA9NeONLLrqjChHtYwO8eh/LrMJlTkFHielW"
  }
  </code></pre>