Spring @RequestPart multipart/mixed Object errors

2.5k views Asked by At

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.

2

There are 2 answers

0
Horanol On

add

Content-Type: application/json;

under the line of

Content-Disposition: form-data; name="fileParameters"

to explicit how to resolve your parameters

see spring docs here

0
Kaifei kaifei On

I have met the same issue with you! I changed the @RequestPart to @Multipart, the issue was fixed. hope it can help for you!