0

I want to save an entity in DB. The entity has several fields and photo (photo I will save in byte[]). I wrote a RestController, but it hasn't worked. Format JSON, I use postman, in form-data I add a file, in raw I put body and use JSON.

@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = {"multipart/form-data"})
@ResponseBody
public void storeAd(@RequestPart("client") @Valid Client client, @RequestPart("file") @Valid MultipartFile file) throws IOException {
    Client myClient = client;
    byte[] s = file.getBytes();
    int a = s.length;

}

I see error: Resolved exception caused by handler execution:

org.springframework.web.HttpMediaTypeNotSupportedException:
    Content type 'application/json' not supported

Request from postman1

  • Hello and welcome to SO! I would start by checking the data coming in. HttpMediaTypeNotSupportedException is just what it says, wrong type of data coming in – Clomez Nov 29 '18 at 10:20
  • You can try and use, "consumes = MediaType.MULTIPART_FORM_DATA_VALUE". – Nenad Vichentikj Nov 29 '18 at 10:29
  • I tried "consumes = MediaType.MULTIPART_FORM_DATA_VALUE" and saw org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported –  Nov 29 '18 at 10:35
  • It's a duplicate. Please find how to send mixin requests here: https://stackoverflow.com/a/25183266/3439496 – Dzmitry Bahdanovich Nov 29 '18 at 10:38

1 Answers1

0

You need to add "Content-Type : application/x-www-form-urlencoded" in header section.

SAN
  • 107
  • 7