I am attempting to upload a file with additional parameters using RequestParts. I have the file uploading correctly; however, when I try and add in the additional parameters I get an error in response.
My Controller:
@RequestMapping(value = "/v1/cases/{caseId}/file", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Success uploadFile(
@RequestPart(value="file") MultipartFile file,
@RequestPart(value="fileParameters") FileParameters fileParameters) throws FileNotFoundException, IOException {
I have tried to POST to this 2 different ways with different errors:
1)
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="file"; filename="myFile"
Content-Type:
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="fileParameters"
{"filePassword":"testPassword", "configuration":{}, "target":null}
----WebKitFormBoundaryE19zNvXGzXaLvS5C
this errors with:
The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method. See 'supportedMediaTypes' in 'additionalInfo' for a list of supported types
2)
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="file"; filename="myFile"
Content-Type:
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="fileParamters[filePassword]"
testPassword
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="fileParamters[configuration]"
{}
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="fileParamters[target]"
null
----WebKitFormBoundaryE19zNvXGzXaLvS5C
which returns the following error:
"rootExceptionClass": "org.springframework.web.multipart.support.MissingServletRequestPartException",
"rootExceptionMessage": "Required request part 'keyParameters' is not present."
I'm assuming the first approach is the correct one; however, the application does support JSON, so I'm not sure what I am missing configuration wise. Is there something I have to add to the request for this to work correctly, or am I missing something in a message converter.
Note: Not sure this matters but I am using Postman to test the endpoint.
add
under the line of
to explicit how to resolve your parameters
see spring docs here