Java Spring: Content type ‘multipart/form-data;boundary ;charset=UTF-8’ not supported

javamultipartform-dataspring

I have created a controller:

@RequestMapping(value = "/photo/" , method = RequestMethod.POST)
public @ResponseBody
void addPhotoData(@RequestBody Photo photo, @RequestParam("data")
        MultipartFile photoData) {

    InputStream in = null;
    try {
        in = photoData.getInputStream();
        photoService.save(photo, in);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

and I send the request with Postman:enter image description here
enter image description here

I cannot understand why I receive the error 415 not supported.
Help!

Best Answer

Try wrapping the request body into an object.

 public class Payload {
   private String name;
   private String url;
   private MultipartFile data;
...
}

Add consumes = { "multipart/form-data" } and

@RequestMapping(value = "/photo/" , method = RequestMethod.POST, consumes = { "multipart/form-data" })
public @ResponseBody void addPhotoData(@ModelAttribute Payload payload) {
...

}

There is also MediaType.MULTIPART_FORM_DATA_VALUE constant instead of using that string

Related Topic