I have a file-upload controller with a method that looks like this:
@RequestMapping(value = "/upload", method = RequestMethod.POST, produces = {"*/*", "application/json"})
public @ResponseBody ScriptUploadResponse upload(@RequestParam("userId") Long userId, @RequestParam("script") MultipartFile file) {
return scriptService.upload(userId, file);
}
This used to work fine in Spring 3 with an XML-based config. I recently moved over to a Java-based config with Spring 4. When I upload a file, I get a 400: Bad request
with the complaint that userId
has not been provided. But when I look at the request in the browser, this is what I see:
------WebKitFormBoundaryoJhTJ817NockqUSY
Content-Disposition: form-data; name="userId"
1
------WebKitFormBoundaryoJhTJ817NockqUSY
Content-Disposition: form-data; name="script"; filename="script.js"
Content-Type: application/javascript
------WebKitFormBoundaryoJhTJ817NockqUSY--
Spring claims:
HTTP Status 400 - Required Long parameter 'userId' is not present
Why is Spring saying that I haven't provided userId
when the payload shows that it is present?
UPDATE
I've put breakpoints inside RequestParamMethodArgumentResolver.java
(an internal Spring class) and I can see that getParts()
on the HttpServletRequest
object returns no parts at all. I'm not sure why this is happening, but it seems to be the root of the issue. From the browser I can see the request being made, but for whatever reason, the multipart data isn't making it through.