Issue
Im trying to post data from postman. I have an method receiving RequestBody and Multipart file. But im steel having this error. The way i use to store images, is working in other implementations when i save only one image.
{
"timestamp": "2020-02-06T19:52:12.566+0000",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'multipart/form-data;boundary=--------------------------833603313116090653834108;charset=UTF-8' not supported",
}
@PostMapping(value = "new", headers=("content-type=multipart/*"), consumes = "multipart/form-data" )
private Product save(@RequestBody Product product, @RequestParam("files") MultipartFile[] files){
var disk = new Disk("product");
Product productSaved = new Product();
String fileName;
try {
if (files != null && files.length >0) {
productSaved = service.save(product);
for (MultipartFile file : files) {
fileName = disk.saveImage(file);
Images image = new Images(fileName, productSaved);
imagesService.saveImage(image);
}
} else {
return null;
}
} catch (IOException e) {
e.printStackTrace();
}
return productSaved;
}
Solution
The first thing is the MultipartFile cannot be inside the body of the Response from your client. The second thing is the data response from your browser should only be only one way.
My suggestion is you can put all the data you want to save to the database inside a form submission. And use @ModelAttribute Product product, @RequestParam MultipartFile[] files
.
Also, your method can be void
since you save data, you don't need to return anything.
Note: @ModelAttribute can be omitted.
Answered By - SongChay Beng
Answer Checked By - Cary Denson (JavaFixing Admin)