3

I am using spring boot version = 1.5.2.RELEASE. When I am sending multi part file with json object to upload file in postman, It throwing 415 Unsupported Media Type exception.

This is my controller class.

@RestController
@RequestMapping("/service/promotion/")
public class JobController {
....
....
....
@RequestMapping(value = "/job/test", method = RequestMethod.POST, produces = "application/json", consumes = "multipart/form-data")
public ResponseEntity<Object> createJobTest(@Valid @RequestBody JobRequest jobRequest,
            @RequestParam(value = "file", required = false) MultipartFile multiPartFile) throws Exception {

My json request class.

 public class JobRequest {
        private String campaignKey;
        private String communicationId;
        private Integer channelId;
        private String templateType;
        private String subject;
        private String frequencyControl;
        private Integer leadsRequested;
        private String keywordRelavance;
        private String scheduledAt;
        private String file;
        private String updatedBy;
        
        //getter and setter
    }   

Json request in postman enter image description here

Multipart file request in postman enter image description here

Header Content-type enter image description here

But when I removed consumes from controller class and from postman as well like

@RequestMapping(value = "/job/test", method = RequestMethod.POST, produces = "application/json")

then debugger coming in controller class but multi part file value coming null in request object like

enter image description here

I googled a lot there are many similar questions which already posted but none of them helped me. Please help me to sort out this mystery.

Thank you.

Community
  • 1
  • 1
Nizamuddin
  • 149
  • 1
  • 2
  • 16

1 Answers1

0

Check this File upload along with other object in Jersey restful web service

Another way is you can pass whole object in text like you are passing file in form-data and convert in object.

 @RequestMapping(value = "/uploadDocs", method = RequestMethod.POST, produces = "application/json", consumes = "multipart/form-data")
    public ResponseEntity<Object> methodName(@RequestParam("files") MultipartFile file, @RequestParam("anyKeyName") String objectString)

Than you can convert string to object using

Class object = new ObjectMapper().readValue(objectString, Class.class);
Harshit
  • 19
  • 1