Issue
So I'm currently building a Multipart-Fileupload with Spring. I want to create my Controller as generic as it can be to enable a good start for my own modulized-controller-application. That's why I'm working with @RequestParam Map<String, Object> requestParams
. So my dummy function looks like this:
@RequestMapping(path = "uploadtest", method = POST, consumes = MULTIPART_FORM_DATA_VALUE)
public String test(@RequestParam("file") MultipartFile file,
@RequestParam Map<String, Object> requestParams) {
return "/[...]";
}
Now when I post a file, I would asume, that it would be available through the file
-variable (works, ok.) and through requestParams.get("file")
(doesn't work).
So here is my question:
Is this intendet to work like this, or is there just no multipart-support enabled for the general @RequestParams
annotation (=Bug/Feature?).
Solution
A multipart request consist of several parts. In case of multipart/form-data
the first part is supposed to consist of parameter / value pairs that are also available as @RequestParam
. The other parts are the files.
When you have @RequestParam MultipartFile file
Spring knows that you want the part that is associated with the file
parameter. Since the file is a part you can also use @RequestPart("file") MultipartFile file
.
Keep in mind that request parameters are Strings. So Map<String, Object>
is basically the same as Map<String, String>
. Therefore requestParams.get("file")
will give you a String value of the file
parameter from the first part of the request.
"I want to create my Controller as generic as it can be" You rather want it as specific as it can be. If you want to keep things generic, then don't use controllers in the first place.
Answered By - a better oliver