I have a Controller like this and I want to submit a form with file uploading as well as some form data like label as shown below. Also, I want to do that using @RequestBody
I couldn't find a way to use @RequestBody.
However, you can do something like this:
@RequestMapping(value = "/uploadStuff", method = RequestMethod.POST)
public MyViewDto doStuff(@RequestPart("json") @Valid MyDto dto,
@RequestPart("file") MultipartFile file) { ... }
You can test it like this:
MockMultipartFile jsonFile = new MockMultipartFile("json", "",
"application/json", "{}".getBytes());
MockMultipartFile dataFile = new MockMultipartFile("file", "foo.zip", "application/octet-stream", bytes);
mockMvc.perform(fileUpload("/uploadStuff")
.file(dataFile)
.file(jsonFile))
.andExpect(status().isOk());