Sun*_*mar 65 spring json spring-mvc
我想使用Spring MVC发布带有一些JSON数据的文件.所以我开发了一个休息服务
@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);
}
Run Code Online (Sandbox Code Playgroud)
当我从其他客户端发送请求时
content-Type = multipart/form-data or multipart/mixed
,我得到下一个异常:
org.springframework.web.multipart.support.MissingServletRequestPartException
任何人都可以帮我解决这个问题吗?
我可以使用@RequestPart
Multipart和JSON发送到服务器吗?
Sun*_*mar 165
这就是我用JSON数据实现Spring MVC Multipart Request的方法.
基于Spring 4.0.2 Release中的RESTful服务,可以使用@RequestPart实现HTTP请求,第一部分为XML或JSON格式数据,第二部分为文件.以下是示例实现.
Controller中的Rest服务将混合使用@RequestPart和MultipartFile来提供此类Multipart + JSON请求.
@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);
}
Run Code Online (Sandbox Code Playgroud)
创建一个FormData对象.
使用以下步骤之一将文件附加到FormData对象.
formData.append("file", document.forms[formName].file.files[0]);
formData.append("file", myFile, "myfile.txt");
要么formData.append("file", myBob, "myfile.txt");
使用字符串化的JSON数据创建blob并将其附加到FormData对象.这会导致多部分请求中第二部分的Content-type为"application/json"而不是文件类型.
将请求发送到服务器.
请求详细信息:
Content-Type: undefined
.这会导致浏览器将Content-Type设置为multipart/form-data并正确填充边界.手动将Content-Type设置为multipart/form-data将无法填写请求的边界参数.
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"
}));
Run Code Online (Sandbox Code Playgroud)
method: "POST",
headers: {
"Content-Type": undefined
},
data: formData
Run Code Online (Sandbox Code Playgroud)
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--
Run Code Online (Sandbox Code Playgroud)
moh*_*ohi 10
这必须奏效!
客户(角度):
$scope.saveForm = function () {
var formData = new FormData();
var file = $scope.myFile;
var json = $scope.myJson;
formData.append("file", file);
formData.append("ad",JSON.stringify(json));//important: convert to JSON!
var req = {
url: '/upload',
method: 'POST',
headers: {'Content-Type': undefined},
data: formData,
transformRequest: function (data, headersGetterFunction) {
return data;
}
};
Run Code Online (Sandbox Code Playgroud)
后端 - 弹簧启动:
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody
Advertisement storeAd(@RequestPart("ad") String adString, @RequestPart("file") MultipartFile file) throws IOException {
Advertisement jsonAd = new ObjectMapper().readValue(adString, Advertisement.class);
//do whatever you want with your file and jsonAd
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
87298 次 |
最近记录: |