简体   繁体   English

将多部件体转换为 java object

[英]Convert multipart body to java object

I'm trying to use java + camel to read a multipart file and converting it to object to process it.我正在尝试使用 java + camel 来读取多部分文件并将其转换为 object 来处理它。 My multipart file is composed of 2 kind of datas: an application/json and an application/octet-stream Is there a way to convert it to some kind of object that i can work with?我的多部分文件由两种数据组成:一个应用程序/json 和一个应用程序/八位字节流有没有办法将它转换为某种我可以使用的 object? basically I'm sending a get REST request to an application that respondes with a multipart file.基本上,我正在向使用多部分文件响应的应用程序发送 get REST 请求。 I can't figure it out how to convert the response body: the application/json to my POJO and the application/octet-stream to a DataHandler我不知道如何转换响应正文:应用程序/json 到我的 POJO 和应用程序/八位字节流到 DataHandler

To handle multipart in camel, you can use unmarshal().mimeMultipart() .要在骆驼中处理多部分,您可以使用unmarshal().mimeMultipart() After this line, Camel Exchange contains an AttachmentMessage , the body of it is the first part of the multipart.在这一行之后,Camel Exchange 包含一个AttachmentMessage ,它的主体是多部分的第一部分。 The next parts can be obtained by calling attachmentMessage.getAttachmentObjects() in Camel processor.接下来的部分可以通过在 Camel 处理器中调用attachmentMessage.getAttachmentObjects()来获得。 For more information on how unmarshal().mimeMultipart() works, see the source code of method unmarshall() in org.apache.camel.dataformat.mime.multipart.MimeMultipartDataFormat .有关unmarshal().mimeMultipart()工作原理的更多信息,请参阅org.apache.camel.dataformat.mime.multipart.MimeMultipartDataFormat中方法unmarshall()的源代码。 Further actions depend on your case.进一步的行动取决于您的情况。 For example, if the first part of a multipart always contains a json object of your SimplePojo class, then you can use unmarshal().json(SimplePojo.class) immediately after unmarshal().mimeMultipart() .例如,如果 multipart 的第一部分始终包含SimplePojo class 的 json object,那么您可以在 json 之后立即使用unmarshal().json(SimplePojo.class) unmarshal().mimeMultipart() Then Camel body will contain your pojo.然后骆驼身体将包含您的pojo。 DataHandlers for the next parts can be obtained as follows:下一部分的 DataHandlers 可以通过以下方式获得:

DataHandler dataHandler = attachmentObjects.get("test.txt").getDataHandler(); 
// test.txt comes from the filename field of the Content-Disposition header in case you have multipart/form-data.

Below is an example of processing a response from a REST service located at {{http.url}}, in a multipart format of the following form:以下是处理来自位于 {{http.url}} 的 REST 服务的响应的示例,采用以下形式的多部分格式:

--Boundary_2411_1961957611_1491990591774
Content-Disposition: form-data; name="part1"
Content-Type: application/json; charset=utf-8 

{
  "id": 123,
  "name": "simple pojo"
}
--Boundary_2411_1961957611_1491990591774
Content-Disposition: form-data; name="part2"; filename="test.txt"
Content-Type: application/octet-stream

test
--Boundary_2411_1961957611_1491990591774--

Camel RouteBuilder:骆驼路线建设者:

from("direct:start")
    .setHeader(Exchange.HTTP_METHOD, constant(HttpMethods.GET))
    .to("{{http.url}}")
    .unmarshal().mimeMultipart()
    .unmarshal().json(SimplePojo.class)
    .process(exchange -> {

        AttachmentMessage attachmentMessage = exchange.getMessage(AttachmentMessage.class);
        SimplePojo simplePojo = attachmentMessage.getBody(SimplePojo.class);

        Map<String, Attachment> attachmentObjects = attachmentMessage.getAttachmentObjects();
        DataHandler dataHandler = attachmentObjects.get("test.txt").getDataHandler();

    })

.to("mock:end");

SimplePojo:简单的Pojo:

public class SimplePojo {
    private Long id;
    private String name;
    //...
    //getters, setters
}

In order for unmarshal().mimeMultipart() and unmarshal().json(SimplePojo.class) to work, you must have dependencies:为了使unmarshal().mimeMultipart()unmarshal().json(SimplePojo.class)工作,您必须具有依赖项:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-mail</artifactId>
    <version>${camel.version}</version>
</dependency>

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jackson</artifactId>
    <version>${camel.version}</version>
</dependency>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在java中解析Multipart /与Multipart / Alternative主体混合 - Parsing Multipart/Mixed with Multipart/Alternative body in java 在 Java 中获取带有正文/多部分表单数据的请求 - GET Request with body/multipart form data in Java 如何将 HTTP 请求正文转换为 JSON Object 中的 ZD52387880E1EA22817A92D71 - How to convert HTTP Request Body into JSON Object in Java Spring Boot可以将Xml响应主体转换为Java对象,但是不能将Java对象转换为Xml请求主体 - Spring Boot can convert Xml response body to Java object, but can't convert Java object to Xml request body 将Spring Multipart对象附加到Java邮件对象 - Attach Spring Multipart object to a Java mail object 在Java中解析包含multipart / form-data请求体的String - Parse a String containing multipart/form-data request body in Java 使用 Java 在 AWS Lambda 上解析 multipart/form-data Body - Parse multipart/form-data Body on AWS Lambda in Java 为什么我不能将 JSON (GSON) 日期格式从改造请求正文转换为 java 对象? - why i can not convert JSON (GSON) date format to java object from retrofit request body? 将图像(字节数组)转换为多部分文件(jpg格式)Java - convert Image(byte array ) into multipart file (jpg format) Java Android Marshmallow之上的翻新Multipart API出现错误java.lang.IllegalStateException:Multipart主体必须至少包含一部分 - Retrofit Multipart API above Android Marshmallow getting error java.lang.IllegalStateException: Multipart body must have at least one part
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM