带有JSON的Spring MVC分段请求 [英] Spring MVC Multipart Request with JSON

查看:138
本文介绍了带有JSON的Spring MVC分段请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用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 or 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版本中的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中的其余服务将混合使用@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)代码段:

  1. 创建一个FormData对象.

  1. Create a FormData object.

使用以下步骤之一将文件追加到FormData对象.

Append the file to the FormData object using one of the below steps.

  1. 如果已使用文件"类型的输入元素上传了文件,则将其附加到FormData对象. formData.append("file", document.forms[formName].file.files[0]);
  2. 将文件直接附加到FormData对象. formData.append("file", myFile, "myfile.txt");formData.append("file", myBob, "myfile.txt");
  1. If the file has been uploaded using an input element of type "file", then append it to the FormData object. formData.append("file", document.forms[formName].file.files[0]);
  2. Directly append the file to the FormData object. formData.append("file", myFile, "myfile.txt"); OR formData.append("file", myBob, "myfile.txt");

  • 使用字符串化的JSON数据创建一个blob,并将其附加到FormData对象.这将导致multipart请求中第二部分的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: undefined.这将导致浏览器将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屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆