How to receive request body data along with multi-part image file?

camelCode :

I want to receive multi-part image file with request body data, but could not able to figure it out, why it is throwing org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported exception

Below is my implementation

public ResponseEntity<GlobalResponse> createUser(@Valid @RequestPart("json") UserDTO userDTO ,@RequestPart(value ="file", required=false)MultipartFile file) throws IOException {

      //Calling some service

      return new ResponseEntity<>( HttpStatus.OK);
}

EDIT: This is my postman configuration

enter image description here

Mushif Ali Nawaz :

Since you're sending data in form-data which can send data in key-value pairs. Not in RequestBody so you need to modify your endpoint like this:

@PostMapping(value = "/createUser")
public ResponseEntity createUser(@RequestParam("json") String json, @RequestParam("file") MultipartFile file) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    UserDTO userDTO = objectMapper.readValue(json, UserDTO.class);
    // Do something
    return new ResponseEntity<>(HttpStatus.OK);
}

You need to receive your UserDTO object in String representation and then map it to UserDTO using ObjectMapper. This will allow you to receive MultipartFile and UserDTO using form-data.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=165352&siteId=1