Spring @RequestPart multipart / mixed Object errors

时间:2015-06-09 20:20:11

标签: spring http-post content-type multipart mixed

我正在尝试使用RequestParts上传包含其他参数的文件。我正确上传文件;但是,当我尝试添加其他参数时,我得到一个错误的响应。

我的控制器:

@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 {

我尝试使用不同的错误以2种不同的方式发布POST:

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

这个错误:

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

返回以下错误:

"rootExceptionClass": "org.springframework.web.multipart.support.MissingServletRequestPartException",
"rootExceptionMessage": "Required request part 'keyParameters' is not present."

我假设第一种方法是正确的;但是,该应用程序确实支持JSON,所以我不确定我在配置方面缺少什么。有什么我必须添加到请求这个正常工作,或我在消息转换器中丢失的东西。

注意:不确定这是否重要但我使用Postman测试端点。

2 个答案:

答案 0 :(得分:0)

添加

  

Content-Type:application / json;

下的

  

内容 - 处置:表单数据;名称=" fileParameters"

明确如何解决参数

请参阅spring docs here

答案 1 :(得分:0)

我遇到了同样的问题!我将@RequestPart更改为@Multipart,问题已修复。希望它对你有所帮助!

相关问题