Spring @RequestPart multipart / mixed Object Errors

I am trying to load a file with additional parameters using RequestParts. I am downloading the file correctly; however, when I try to add additional parameters, I get an error.

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 tried to POST this in two 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

      

these errors:

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 assume the first approach is correct; however the app does support JSON, so I'm not sure if I'm missing the configuration. Is there something I have to add to the request for this to work correctly, or am I missing something in the message converter.

Note. Not sure if this matters, but I'm using Postman to test the endpoint.

+3


source to share


2 answers


add

Content-Type: application / json;

under the line



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

to indicate how to resolve your options

see spring docs here

0


source


I met the same problem with you! I changed @RequestPart to @Multipart, the problem was fixed. hope this can help you!



0


source







All Articles