我使用的是IntellijIDEA,我有测试SOAP WS的项目。我有发送的xml和msg文件。如何将此文件转换为cxf消息和cxf SoapMessage进行发送?这是我的方法,返回SoapMessage:
public SoapMessage getMessage(File file) throws Exception{
Message msg;
msg.setContent(File.class, file);
SoapMessage message = new SoapMessage(msg);
return message;
}
但如果我尝试调用此方法,我会看到:
显示java.lang.NullPointerException
我尝试发送此消息:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
schemaLocation="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<Test>
<msg>Test</msg>
</Test>
</soapenv:Body>
</soapenv:Envelope>
UPD: msg initiallistion需要什么?
Message msg = new Message(){...}?
用于将my * .msg文件转换为SoapMessage并将其发送到SOAP WS
答案 0 :(得分:0)
您可以通过两种方式测试基于SOAP的Web服务
使用Java的wsimport命令
wsimport -keep http://localhost:9090/ws/hello?wsdl
lean()
使用obj调用适当的方法。
我们不需要手动构建SOAP消息。
答案 1 :(得分:0)
我需要处理类似的事情,这就是我所做的:
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.List;
import org.apache.cxf.binding.soap.Soap12;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@RunWith(BlockJUnit4ClassRunner.class)
public class SOAPMessageTest {
@Test
public void emptyHeaderTest() {
SoapMessage soapMessage = new SoapMessage(Soap12.getInstance());
List<Header> headers = soapMessage.getHeaders();
assertThat(headers.size(), is(0));
}
}
根据需要调整此代码以将内容添加到soapMessage
对象。
答案 2 :(得分:0)
您是否试图实例化soapMessage?
Reader reader = new StringReader(body);
XMLStreamReader xmlReader = null;
XMLInputFactory factory = XMLInputFactory.newInstance(); // Or newFactory()
try {
xmlReader = factory.createXMLStreamReader(reader);
} catch (XMLStreamException e) {
e.printStackTrace();
}
SoapMessage soapMessage = new SoapMessage(Soap11.getInstance());
soapMessage.setContent(XMLStreamReader.class, xmlReader);