【问题标题】:Spring @RequestPart multipart/mixed Object errorsSpring @RequestPart 多部分/混合对象错误
【发布时间】:2015-08-24 20:41:54
【问题描述】:

我正在尝试使用 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 {

我尝试过以两种不同的方式发布不同的错误:

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 测试端点。

【问题讨论】:

  • 嗨,你知道了吗?我也遇到了同样的问题。我查看了许多解决方案,但似乎都没有。
  • 不幸的是,我从来没有这样做过。我刚刚将请求正文作为请求参数中的 json 字符串传递,并使用 jackson 将其放入我需要的对象中。远非理想。
  • 老问题,我不确定是否是问题,但仍会发表评论。您的“fileParameters”作为表单数据发布。也许尝试发布为 application/json

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


【解决方案1】:

添加

Content-Type: application/json;

行下

内容配置:表单数据;名称="文件参数"

明确如何解决您的参数

参见春季文档here

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-04-20
      • 2019-07-21
      • 1970-01-01
      • 1970-01-01
      • 2011-04-23
      • 2016-11-08
      • 2016-11-07
      • 2017-08-29
      • 2017-01-14
      相关资源
      最近更新 更多