【问题标题】:MissingServletRequestPartException request part is not presentMissingServletRequestPartException 请求部分不存在
【发布时间】:2018-02-03 20:21:26
【问题描述】:

我的 Rest Web 服务收到一个 Json 对象和 Multipartfile,

@RequestMapping(value = "saveWithDoc", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> saveWithDoc(@RequestPart Document document,
        @RequestPart(required = false) MultipartFile doc) throws ServletException, IOException {
        ...
    return DocumentService.saveWithDoc(document, doc);
}

我正在使用 Rest Advanced Client 进行尝试,但没有成功。 我想我正在发送 Web 服务所需的对象文档,并在我选择的选项卡文件中的文件。

在我的服务器中我有这个例外:

{ “时间戳”:1503683667950,“状态”:400,“错误”:“错误请求”, “例外”: “org.springframework.web.multipart.support.MissingServletRequestPartException”, “消息”:“所需的请求部分'文档'不存在”,“路径”: "/myproject-ws/api/Document/saveWithDoc" }

在我的请求中,我有这个:

POST /myproject-ws/api/Document/saveWithDoc HTTP/1.1 主机: 本地主机:8490 内容长度:511 内容类型:多部分/表单数据; 边界=----WebKitFormBoundary2ppEQ78lS5rcLD9g cookie: JSESSIONID=x-MM0NMFbWD849tDvD07hzXykWyq4TrBuvq2BLEK.andres-hp-250-g4-notebook-pc; XSRF-TOKEN=a27b10b4-1ee1-4434-b3a5-4f6037a19561 x-xsrf-token: a27b10b4-1ee1-4434-b3a5-4f6037a19561

------WebKitFormBoundary2ppEQ78lS5rcLD9g Content-Disposition: form-data;名称=“文档”;文件名="claves" 内容类型: 应用程序/八位字节流

文档中的文本

fb34cee6-2366-4335-9952-26a31d4ddc28

------WebKitFormBoundary2ppEQ78lS5rcLD9g Content-Disposition: form-data;名称="文档"

{"配置":{"id":1 }} ------WebKitFormBoundary2ppEQ78lS5rcLD9g--

会有什么问题?提前致谢!

编辑的 Rest Api:

@RequestMapping(value = "saveWithDoc", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> saveWithDoc(@RequestPart(value="document") Document document,
        @RequestPart(value="doc", required = false) MultipartFile doc) throws ServletException, IOException {
    ....
    return personDocumentService.saveWithDoc(personDocument, doc);
}

【问题讨论】:

  • 您能否也分享您更新的控制器代码?
  • Rest Controller 是同一个伙伴,我想评论一下我已经用 Postman 测试过但没有成功!我得到同样的错误。
  • 你需要把你的控制器的签名改成@RequestPart("document") Document document, @RequestPart(value = "doc", required = false) MultipartFile doc
  • 难以置信....我已经编辑了控制器,但我在 Postman 和 Rest Advanced Client 中遇到了同样的问题。
  • 您的代码库中是否存在 commons-fileupload 依赖项或 jar?和 Content-Type: application/json 应该在这样的文档部分中可用 - ------WebKitFormBoundary2ppEQ78lS5rcLD9g Content-Type: application/json Content-Disposition: form-data; name="document" {"Config":{"id":1 }} ------WebKitFormBoundary2ppEQ78lS5rcLD9g--

标签: spring


【解决方案1】:

你需要设置 contentType 与边界类似

Content-Type: multipart/form-data; boundary=e6e95273-cafb-4dbf-86b8-a1c0ed85b5c5

对于我的一个应用,JSON 看起来像 -

`--e6e95273-cafb-4dbf-86b8-a1c0ed85b5c5
 Content-Type: application/json
 Content-Disposition: form-data; name="bulkAction"; filename="blob"

 {
     "operation": "notification"        
 }`

--e6e95273-cafb-4dbf-86b8-a1c0ed85b5c5
Content-Type: text/csv
Content-Disposition: form-data; name="file"; filename="myfile1.csv"
Content-Transfer-Encoding: base64

<Base64OfDocument>
--e6e95273-cafb-4dbf-86b8-a1c0ed85b5c5--

我的 RestController 代码将类似于端点 POST /proserv/account/{accountId}/envelopes/bulk

@RequestMapping(value = "/account/{accountId}/envelopes/bulk", method = RequestMethod.POST, consumes = {
        "multipart/form-data" })
@ResponseBody
public EnvelopeUpdateStatus bulkUpdateService(@RequestPart("bulkAction") BulkAction bulkAction, @PathVariable String accountId,
        HttpServletRequest request, @RequestPart("file") MultipartFile... files) {

}

在我的代码中 MultipartFile... 表示代码可以包含多个文档。我已经使用 POSTMAN 客户端测试了这个 REST 调用。

方法中的

@RequestPart("bulkAction") 来自 JSON 部分(名称应匹配),@RequestPart("file") 是实际文档(在我的情况下是 csv),(名称应匹配)

【讨论】:

  • 请您检查已编辑的问题,谢谢,我现在使用的是 Rest Advanced Client,但我之前尝试过 Postman。
猜你喜欢
  • 1970-01-01
  • 2020-04-29
  • 2022-01-13
  • 2017-10-23
  • 2017-12-01
  • 1970-01-01
  • 2019-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多