我需要能够克隆SOAPMessage对象,但是它不实现Clonable。我怎样才能做到这一点? [英] I need to be able to clone a SOAPMessage object, but it doesn't implement Clonable. How can I do this?

查看:75
本文介绍了我需要能够克隆SOAPMessage对象,但是它不实现Clonable。我怎样才能做到这一点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个骨架 SOAPMessage对象,它将是其他SOAPMessages的起点。我希望能够克隆基本的SOAPMessage,因此可以在其中填充所需的信息并将其发送,并在每次需要发送SOAPMessage时进行此操作。

如何克隆SOAPMessage ,因为它没有实现Clonable?
我一直在寻找其他框架,并且发现了Apache CXF,它具有一个可克隆的SoapMessage类。但是,我无法使用它,因为我使用的WSDL是旧式的(编码的或类似的东西),并且不允许我将WSDL导入Java类中。

任何建议?

I have a "skeleton" SOAPMessage object, which will be the starting point of other SOAPMessages. I want to be able to clone the base SOAPMessage, so I can then fill it with the information needed and send it, and do this every time I need to send a SOAPMessage.
How can I clone a SOAPMessage, since it doesn't implement Clonable? I've been looking for other frameworks and found Apache CXF, which has a SoapMessage class that is Clonable. However, I can't use it because the WSDL I'm using is old-style (encoded, or something) and doesn't allow me to import the WSDL into Java classes...
Any suggestions? Thanks in advance!

推荐答案

您只需将 SOAPMessage 更改为字符串,并将此字符串用作新实例消息的来源。

You can simply change SOAPMessage to string, and use this string as a source for new instance message.

下面的示例。

public static String messageToString(SOAPMessage soap) throws  SOAPException, IOException{
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    soap.writeTo(stream);
    String message = new String(stream.toByteArray(), "utf-8");
    return message;
}

public static SOAPMessage stringToMessage(String soap) throws IOException, SOAPException{
    InputStream inputStream = new ByteArrayInputStream(soap.getBytes());
    SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(null, inputStream);
    return soapMessage;
}

SOAPMessage sourceMessage; //some message to clone need to initiate it
SOAPMessage clonedMessage = MessageFactory.newInstance().createMessage();
clonedMessage = stringToMessage(messageToString(sourceMessage));

为我工作。

这篇关于我需要能够克隆SOAPMessage对象,但是它不实现Clonable。我怎样才能做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆