Spring Boot Multipart/相关的mime类型支持 [英] Spring boot multipart/related mime type support

查看:178
本文介绍了Spring Boot Multipart/相关的mime类型支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用一个内容类型为多部分/相关的请求.请求将包含图像,json有效内容和二进制内容.我试图找到一些有关如何在Spring Boot应用程序中处理此类请求的示例,我找到了一些有关如何处理multipart/form-data请求的参考,但与multipart/相关的mime类型无关.

I need to consume a request whose content-type is multipart/related. Request will consist of image, json payload and binary content. I tried to find some example on how to handle such request in a Spring boot application, I found some references on how to handle multipart/form-data request but nothing related to multipart/related mime type.

请求是这样的:

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 Boot应用程序中处理此请求.我正在使用JaxRS.

Can someone tell on how to handle this request in Spring boot application. I'm using JaxRS.

推荐答案

为解决此问题,我首先提到了 http://cxf.apache.org/docs/jax-rs-multiparts.html 以正确理解与JAX-RS相关的multipart/related.接下来,我参考了 Jersey文档,我构建了以下测试项目: https://github.com/ShawnTuatara/stackoverflow-38838926 .主要示例是:

To solve this I first referred to 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/stackoverflow-38838926. The main example is:

package ca.tuatara.stackoverflow;

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 Stackoverflow38838926Application {
    public static void main(String[] args) {
        SpringApplication.run(Stackoverflow38838926Application.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日志记录,以便您可以准确查看测试运行时线路上发生了什么.

The test shows how to send the multipart requests. I have kept in some DEBUG logging so you can see exactly what is going over the wire when the test runs.

您的原始POST有效负载存在几个问题,无法对其进行正确解析.内容的标题和内容之间必须有换行符.如果不为Content-Disposition提供名称"属性,则只能使用第一个示例("/upload").如果确实为表单数据命名,则可以使用第二个示例("/uploadParts").我没有对图像或文件上传进行示例,但是如果您阅读Jersey的多部分页面,您会发现直接在request方法上添加该参数输入是很容易的.

There was a couple issues with your original POST payload that won't allow it to be parsed properly. The content has to have a newline between the headers and the content. If you don't provide a "name" property for the Content-Disposition then you can only use the first example ("/upload"). If you do name the form-data then you can use the second example ("/uploadParts"). I didn't do the example with the image or file upload but if you read the Jersey multipart page you can see it is straightforward to add that parameter input on the request method.

这篇关于Spring Boot Multipart/相关的mime类型支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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