【发布时间】: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