Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 518
Stefano Maglione Avatar asked Mar 26 '20 19:03

Stefano Maglione


2 Answers

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

like image 64
dondragon2 Avatar answered Oct 16 '22 07:10

dondragon2


This is called multipart mixed type. Try changing your signature like this

@RequestMapping(value = "/photo/" , method = RequestMethod.POST, consumes = {"multipart/mixed"})
public @ResponseBody void addPhotoData(@RequestPart Photo photo, @RequestPart("data")
        MultipartFile photoData) {
like image 1
pvpkiran Avatar answered Oct 16 '22 07:10

pvpkiran