In my Spring MVC server I want to receive a multipart/form-data request containing both a file (an image) and some JSON metadata. I can build a well-formed multipart request whe
If you use the @RequestPart
annotation instead of @RequestParam
, it will actually pass the parameters through the message converters.
So, if you change your controller method to the following, it should work as you describe:
@RequestMapping(value = MY_URL, method=RequestMethod.POST, headers="Content-Type=multipart/form-data")
public void myMethod(@RequestParam("image") MultipartFile file, @RequestPart("json") MyClass myClass) {
...
}
You can read more about it in the Spring reference guide: http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/mvc.html#mvc-multipart-forms-non-browsers