Spring 启动 multipart/related mime 类型支持

Spring boot multipart/related mime type support

我需要消费一个内容类型为multipart/related的请求。请求将包含图像、json 负载和二进制内容。我试图找到一些关于如何在 Spring 引导应用程序中处理此类请求的示例,我找到了一些关于如何处理 multipart/form-data 请求但与 multipart/related mime 类型无关的参考资料。

请求是这样的:

POST /upload
Content-Type: multipart/related; boundary="123asdf234"
--123asdf234
Content-Type: application/json; charset=UTF-8
Content-Disposition: form-data
{
    "json": "payload"

}
—-123asdf234
Content-Type: application/zip
Content-Disposition: file-data; filename="some.zip"; size=123456;
<binary-attachment-content>
—-123asdf234
Content-Type: image/png
Content-Disposition: file-data; filename="image1.png"; size=123456;
<binary-attachment-content>
—-123asdf234-—

谁能告诉我如何在 Spring 启动应用程序中处理这个请求。我正在使用 JaxRS。

为了解决这个问题,我首先提到了http://cxf.apache.org/docs/jax-rs-multiparts.html to properly understand multipart/related in relation to JAX-RS. Next I referred to the Spring documentation on JAX-RS and chose to use the Jersey dependency to solve it. Then referring to the Jersey documentation I build the following test project: https://github.com/ShawnTuatara/Whosebug-38838926。主要例子是:

package ca.tuatara.Whosebug;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.media.multipart.BodyPart;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class Whosebug38838926Application {
    public static void main(String[] args) {
        SpringApplication.run(Whosebug38838926Application.class, args);
    }

    @Component
    public class JerseyConfig extends ResourceConfig {
        public JerseyConfig() {
            register(MultiPartFeature.class);
            register(MultipartHandler.class);
            register(MultipartPartsHandler.class);
        }
    }

    @Component
    @Path("/upload")
    @Consumes("multipart/*")
    @Produces("text/text")
    public class MultipartHandler {
        @POST
        public String upload(MultiPart request) {
            StringBuffer response = new StringBuffer();
            for (BodyPart part : request.getBodyParts()) {
                if (MediaType.APPLICATION_JSON_TYPE.isCompatible(part.getMediaType())) {
                    response.append(part.getEntityAs(JsonModel.class));
                } else if (MediaType.APPLICATION_XML_TYPE.isCompatible(part.getMediaType())) {
                    response.append(part.getEntityAs(XmlModel.class));
                }
                response.append(System.lineSeparator());
            }
            return response.toString();
        }
    }

    @Component
    @Path("/uploadParts")
    @Consumes("multipart/*")
    @Produces("text/text")
    public class MultipartPartsHandler {
        @POST
        public String upload(@FormDataParam("json") JsonModel json, @FormDataParam("xml") XmlModel xml) {
            return json + System.lineSeparator() + xml;
        }
    }
}

测试展示了如何发送多部分请求。我保留了一些 DEBUG 日志记录,这样您就可以在测试运行时准确地看到网络上发生了什么。

您的原始 POST 负载存在一些问题,无法正确解析。内容必须在 headers 和内容之间有换行符。如果您没有为 Content-Disposition 提供 "name" 属性,那么您只能使用第一个示例(“/upload”)。如果您确实命名 form-data,那么您可以使用第二个示例(“/uploadParts”)。我没有用图像或文件上传来做这个例子,但是如果你阅读 Jersey 多部分页面,你会发现在请求方法上添加参数输入是很简单的。