使用 JSON 的 Spring MVC 多部分请求 [英] Spring MVC Multipart Request with JSON

查看:44
本文介绍了使用 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 或 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服务,第一部分为XML或JSON格式数据,第二部分为文件的HTTP请求可以通过@RequestPart实现.下面是示例实现.

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) 代码段:

  1. 创建一个 FormData 对象.

  1. Create a FormData object.

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

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

  1. 如果文件是使用file"类型的输入元素上传的,则将其附加到 FormData 对象.formData.append("file", document.forms[formName].file.files[0]);
  2. 直接将文件附加到 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屋!

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