[英]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.