在一次调用中从javascript发送文件,json对象和xml对象

时间:2016-10-21 10:56:56

标签: javascript spring rest http multipartform-data

以下显示了我想要实现的目标,但不起作用:

BACKEND(春天):

@RequestMapping(
    value="/test", 
    method = RequestMethod.POST, 
    consumes = { "multipart/mixed", MediaType.MULTIPART_FORM_DATA_VALUE })
@ResponseBody
public String test(
        @RequestParam("file") MultipartFile file, 
        @RequestParam("xmlObject") XmlObject xmlObject,
        @RequestPart("jsonObject") JsonObject jsonObject) {
    return "Done";
}

FRONTEND:

 var multiPart = new FormData();
 multiPart.append('file', file); //file is a File
 multiPart.append('jsonObject', jsonObject); //jsonObject is a jsObject (auto converted by angular to json)
 multiPart.append('xmlObject', xmlString); //xmlString is a xml-string
 //... sendRequest with multiPart as data and Content-Type undefined -> becomes multipart/form-data; with a boundry

这会产生例外:Failed to convert value of type 'java.lang.String' to required type '...XmlObject'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [...XmlObject]: no matching editors or conversion strategy found"Required request part 'xmlObject' is not present.

我尝试使用内容类型的Blob,但没有成功。

还尝试将RequestParam更改为RequestPart和其他值...

如何正确地做到这一点?

(顺便说一下,这些调用都是单独工作的,但我需要捆绑它们以防止出现不一致的状态)

0 个答案:

没有答案