Jersey reading nested Multipart (multipart/mixed)

我只是一个虾纸丫 提交于 2019-12-13 06:48:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!