带有 JSON 的 Spring MVC 多部分请求 [英] Spring MVC Multipart Request with JSON
问题描述
我想使用 Spring MVC 发布一个包含一些 JSON 数据的文件.所以我开发了一个休息服务作为
I want to post a file with some JSON data using Spring MVC. So I've developed a rest service as
@RequestMapping(value = "/servicegenerator/wsdl", method = RequestMethod.POST,consumes = { "multipart/mixed", "multipart/form-data" })
@ResponseBody
public String generateWSDLService(@RequestPart("meta-data") WSDLInfo wsdlInfo,@RequestPart("file") MultipartFile file) throws WSDLException, IOException,
JAXBException, ParserConfigurationException, SAXException, TransformerException {
return handleWSDL(wsdlInfo,file);
}
当我从其余客户端发送请求时content-Type = multipart/form-data 或 multipart/mixed
,我得到下一个异常:org.springframework.web.multipart.support.MissingServletRequestPartException
When I send a request from the rest client with
content-Type = multipart/form-data or multipart/mixed
, I get the next exception:
org.springframework.web.multipart.support.MissingServletRequestPartException
谁能帮我解决这个问题?
Can anyone help me in solving this issue?
我可以使用 @RequestPart
将 Multipart 和 JSON 发送到服务器吗?
Can I use @RequestPart
to send both Multipart and JSON to a server?
推荐答案
这就是我使用 JSON 数据实现 Spring MVC Multipart Request 的方式.
This is how I implemented Spring MVC Multipart Request with JSON Data.
基于 Spring 4.0.2 Release 中的 RESTful 服务,可以通过 @RequestPart 实现 HTTP 请求的第一部分为 XML 或 JSON 格式的数据,第二部分为文件.下面是示例实现.
Based on RESTful service in Spring 4.0.2 Release, HTTP request with the first part as XML or JSON formatted data and the second part as a file can be achieved with @RequestPart. Below is the sample implementation.
Controller 中的 Rest 服务将混合使用 @RequestPart 和 MultipartFile 来服务此类 Multipart + JSON 请求.
Rest service in Controller will have mixed @RequestPart and MultipartFile to serve such Multipart + JSON request.
@RequestMapping(value = "/executesampleservice", method = RequestMethod.POST,
consumes = {"multipart/form-data"})
@ResponseBody
public boolean executeSampleService(
@RequestPart("properties") @Valid ConnectionProperties properties,
@RequestPart("file") @Valid @NotNull @NotBlank MultipartFile file) {
return projectService.executeSampleService(properties, file);
}
前端 (JavaScript) 代码段:
创建一个 FormData 对象.
Create a FormData object.
使用以下步骤之一将文件附加到 FormData 对象.
Append the file to the FormData object using one of the below steps.
- 如果文件已使用文件"类型的输入元素上传,则将其附加到 FormData 对象.
formData.append("file", document.forms[formName].file.files[0]);
- 直接将文件附加到 FormData 对象.
formData.append("file", myFile, "myfile.txt");
或formData.append("file", myBob, "myfile.txt");
使用字符串化的 JSON 数据创建一个 blob,并将其附加到 FormData 对象.这会导致多部分请求中第二部分的 Content-type 为application/json",而不是文件类型.
Create a blob with the stringified JSON data and append it to the FormData object. This causes the Content-type of the second part in the multipart request to be "application/json" instead of the file type.
向服务器发送请求.
请求详情:内容类型:未定义
.这会导致浏览器将 Content-Type 设置为 multipart/form-data 并正确填充边界.手动将 Content-Type 设置为 multipart/form-data 将无法填写请求的边界参数.
Request Details:
Content-Type: undefined
. This causes the browser to set the Content-Type to multipart/form-data and fill the boundary correctly. Manually setting Content-Type to multipart/form-data will fail to fill in the boundary parameter of the request.
Javascript 代码:
formData = new FormData();
formData.append("file", document.forms[formName].file.files[0]);
formData.append('properties', new Blob([JSON.stringify({
"name": "root",
"password": "root"
})], {
type: "application/json"
}));
请求详情:
method: "POST",
headers: {
"Content-Type": undefined
},
data: formData
请求负载:
Accept:application/json, text/plain, */*
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryEBoJzS3HQ4PgE1QB
------WebKitFormBoundaryvijcWI2ZrZQ8xEBN
Content-Disposition: form-data; name="file"; filename="myfile.txt"
Content-Type: application/txt
------WebKitFormBoundaryvijcWI2ZrZQ8xEBN
Content-Disposition: form-data; name="properties"; filename="blob"
Content-Type: application/json
------WebKitFormBoundaryvijcWI2ZrZQ8xEBN--
这篇关于带有 JSON 的 Spring MVC 多部分请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!