将多部分主体转换为 java 对象 [英] Convert multipart body to java object

查看:28
本文介绍了将多部分主体转换为 java 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 java + camel 读取多部分文件并将其转换为对象以进行处理.我的多部分文件由两种数据组成:一个 application/json 和一个 application/octet-stream有没有办法将它转换为我可以使用的某种对象?基本上,我正在向使用多部分文件响应的应用程序发送 get REST 请求.我不知道如何转换响应体:application/json 到我的 POJO 和 application/octet-stream 到 DataHandler

I'm trying to use java + camel to read a multipart file and converting it to object to process it. 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? basically I'm sending a get REST request to an application that respondes with a multipart file. 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

推荐答案

要在camel中处理multipart,可以使用unmarshal().mimeMultipart().在这一行之后,Camel Exchange 包含一个 AttachmentMessage,它的主体是 multipart 的第一部分.接下来的部分可以通过在 Camel 处理器中调用 attachmentMessage.getAttachmentObjects() 获得.有关 unmarshal().mimeMultipart() 工作原理的更多信息,请参阅 org.apache.camel.dataformat.mime 中方法 unmarshall() 的源代码.multipart.MimeMultipartDataFormat.进一步的行动取决于您的情况.例如,如果 multipart 的第一部分始终包含您的 SimplePojo 类的 json 对象,那么您可以在之后立即使用 unmarshal().json(SimplePojo.class)unmarshal().mimeMultipart().然后 Camel body 将包含您的 pojo.下一部分的DataHandlers可以通过以下方式获得:

To handle multipart in camel, you can use unmarshal().mimeMultipart(). After this line, Camel Exchange contains an AttachmentMessage, the body of it is the first part of the multipart. The next parts can be obtained by calling attachmentMessage.getAttachmentObjects() in Camel processor. For more information on how unmarshal().mimeMultipart() works, see the source code of method unmarshall() in org.apache.camel.dataformat.mime.multipart.MimeMultipartDataFormat. 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(). Then Camel body will contain your pojo. DataHandlers for the next parts can be obtained as follows:

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.

以下是处理来自位于 {{http.url}} 的 REST 服务的响应的示例,采用以下形式的多部分格式:

Below is an example of processing a response from a REST service located at {{http.url}}, in a multipart format of the following form:

--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:

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:

SimplePojo:

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

为了让 unmarshal().mimeMultipart()unmarshal().json(SimplePojo.class) 工作,你必须有依赖:

In order for unmarshal().mimeMultipart() and unmarshal().json(SimplePojo.class) to work, you must have dependencies:

<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>

这篇关于将多部分主体转换为 java 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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