问题
In Jersey I can send multipart/mixed data like this :
MultiPart multipartWrapper = new MultiPart(MultiPartMediaTypes.MULTIPART_MIXED_TYPE);
for (IMessageContainer msgCont : input.getMessages()) {
MultiPart m = new MultiPart(MultiPartMediaTypes.MULTIPART_MIXED_TYPE)
.bodyPart(
new BodyPart(msgCont.getDescription(), MediaType.APPLICATION_JSON_TYPE))
.bodyPart(
new BodyPart(msgCont.getDetails(), MediaType.APPLICATION_OCTET_STREAM_TYPE));
//nest the new multipart into a bodypart within the root multipart
multipartWrapper.bodyPart(new BodyPart(m, MultiPartMediaTypes.MULTIPART_MIXED_TYPE));
}
}
This enveloping multipart/mixed can now be sent over the wire as part of a Response. On the receiving side we can do
MultiPart entity = response.readEntity(MultiPart.class);
List<BodyPart> bodyParts = entity.getBodyParts();
List<IMessageWrapper> rslt = new ArrayList<>();
for(BodyPart bp : bodyParts) {
//how do we get the wrapped Multipart here, so we can
//get into its BodyParts?
}
I feel like I am missing something. How can we get to the Multipart wrapped inside the BodyPart? When examining the BodyPart, it just encloses org.jvnet.mimepull.MimePart.
回答1:
Immediately after posting the answer was clear.
MultiPart nestedMultiPart = (MultiPart)bp.getEntity();
List<BodyPart> msgParts = nestedMultiPart.getBodyParts();
来源:https://stackoverflow.com/questions/43197019/jersey-reading-nested-multipart-multipart-mixed