加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何使用java解析XOP / MTOM SOAP响应?

发布时间:2020-12-14 05:42:59 所属栏目:Java 来源:网络整理
导读:我只想知道,有没有简单的方法来解析MTOM / XOP SOAP响应. 问题是我使用普通HTTP发送soap消息和 javax.xml来解析响应.但是有些服务用mulipart / related响应我,它需要更复杂的逻辑来解析它(性能很重要). 所以我想我可以以某种方式利用apache cxf,apache axiom
我只想知道,有没有简单的方法来解析MTOM / XOP SOAP响应.
问题是我使用普通HTTP发送soap消息和 javax.xml来解析响应.但是有些服务用mulipart / related响应我,它需要更复杂的逻辑来解析它(性能很重要).
所以我想我可以以某种方式利用apache cxf,apache axiom或任何其他库来解析MTOM / XOP SOAP响应?

解决方法

These unit tests将向您展示如何使用CXF从MTOM消息中提取附件.如果将来不存在此链接,我将内联其中一个测试:
private MessageImpl msg;

@Before
public void setUp() throws Exception {
    msg = new MessageImpl();
    Exchange exchange = new ExchangeImpl();
    msg.setExchange(exchange);
}

@Test
public void testDeserializerMtom() throws Exception {
    InputStream is = getClass().getResourceAsStream("mimedata");
    String ct = "multipart/related; type="application/xop+xml"; "
                + "start="<soap.xml@xfire.codehaus.org>"; "
                + "start-info="text/xml; charset=utf-8"; "
                + "boundary="----=_Part_4_701508.1145579811786"";

    msg.put(Message.CONTENT_TYPE,ct);
    msg.setContent(InputStream.class,is);

    AttachmentDeserializer deserializer = new AttachmentDeserializer(msg);
    deserializer.initializeAttachments();

    InputStream attBody = msg.getContent(InputStream.class);
    assertTrue(attBody != is);
    assertTrue(attBody instanceof DelegatingInputStream);

    Collection<Attachment> atts = msg.getAttachments();
    assertNotNull(atts);

    Iterator<Attachment> itr = atts.iterator();
    assertTrue(itr.hasNext());

    Attachment a = itr.next();
    assertNotNull(a);

    InputStream attIs = a.getDataHandler().getInputStream();

    // check the cached output stream
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IOUtils.copy(attBody,out);
    assertTrue(out.toString().startsWith("<env:Envelope"));

    // try streaming a character off the wire
    assertTrue(attIs.read() == '/');
    assertTrue(attIs.read() == '9');
}

在您的情况下,ct将来自响应的内容类型标头. “mimedata”将是回应的内容.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读