1

I have a REST controller method which will take multipart files and JSON object to save as a product with images.

Here is my controller method.

@PostMapping(value = "/{username}/saveProduct", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE})
public void saveProduct(@PathVariable("username") String username,
                        @RequestPart("multipartFiles") List<MultipartFile> multipartFiles,
                        @RequestPart("product") Product product)
{
    Users user = userService.findUserByUsername(username);
    List<Images> listOfImages = productService.getBLOBfromFile(multipartFiles, product);
    product.setImages(listOfImages);
    product.setUser(user);
    user.setProducts(product);
    userService.saveUser(user);
}

For some reason I am getting this error:

"timestamp": "2021-01-18T20:05:32.409+00:00",
"status": 415,
"error": "Unsupported Media Type",
"trace": "org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported\r\n\tat org.

From postman I am sending enter image description here

I tried using @RequestParam and @ModelAttribute as well. Did not work for me.

Also, this method was working when I was writing MVC app.

  • Does this answer your question? [Spring Boot controller - Upload Multipart and JSON to DTO](https://stackoverflow.com/questions/49845355/spring-boot-controller-upload-multipart-and-json-to-dto) – Roar S. Jan 18 '21 at 21:23
  • I already tried those. Did not work for me – Shanks Redemption Jan 18 '21 at 22:47

0 Answers0