0

Basically, I am sending two parameters , one a String and the other a file to my controller in Spring Boot . In the action, when I receive the file first and then the String next, like so

@RequestMapping(value = "/updatemedia", method = RequestMethod.PATCH,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> updateMedia(@RequestParam(value ="file") MultipartFile fileToUpload , @RequestParam(value = "keyId") String keyId )

everything is fine and I am able to access the String and the file correctly.

But when I change the order of the parameters , like so

@RequestMapping(value = "/updatemedia", method = RequestMethod.PATCH,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> updateMedia( @RequestParam(value = "keyId") String keyId , @RequestParam(value ="file") MultipartFile fileToUpload )

and send the params through Postman, I am hitting the below errorenter image description here

I researched a lot but am not able to understand this behaviour.

tarunkt
  • 306
  • 2
  • 12

1 Answers1

0

Because you send keyId in body while it's declared as @RequestParam https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html . @RequestParam has nothing to deal with request body, it's passed in request url. What about your example, the first approach works, because your method expects one @RequestPart, the others are ignored.

Serg Vasylchak
  • 858
  • 4
  • 18
  • 1
    Not so , please have a look at this https://stackoverflow.com/questions/19468572/spring-mvc-why-not-able-to-use-requestbody-and-requestparam-together Also, I posted the question wrong, I was using @RequestParam for both the params – tarunkt Jan 05 '18 at 06:22