简体   繁体   中英

Jersey reading nested Multipart (multipart/mixed)

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.

Immediately after posting the answer was clear.

MultiPart nestedMultiPart = (MultiPart)bp.getEntity();
List<BodyPart> msgParts = nestedMultiPart.getBodyParts();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM