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