If you want to convert the content of a part into a domain object (e.g. a User
or Account
or Address
), then the process is very simple:
It is possible to upload multiple parts, each with a different name. For each part name, you will need one parameter annotated with @RequestPart
, whose name matches the part name.
To receive a file uploaded via an HTTP Post, you need to do the following:
@RequestMapping(
value = "...",
method = RequestMethod.POST,
consumes = MediaType.MULTIPART_FORM_DATA_VALUE
)
public Object uploadFile(
@RequestPart Address address,
) {
.
.
.
}
As a raw HTTP request:
POST /... HTTP/1.1
Host: ...
Content-Type: multipart/form-data; boundary=----------287032381131322
------------287032381131322
Content-Disposition: form-data; name="address"; filename="address.json"
Content-Type: application/json
{"houseNumber": "10/A", "streetName": "Dumbldore Road", "town": "Hogsmede"}
------------287032381131322--
The most important things are:
Content-Type
of the part must be one that Spring would be able to handle if you had sent it as a regular request. That is, if you could perform a POST
to an endpoint with a Content-Type
of foo/bar
, and Spring is able to turn that into an object, then it will also be able to marshal a part into an object.Content-Type
of the part. If you cannot, this approach will not work - Spring will not attempt to guess the Content-Type
of the part.