我使用Apache-CXF编写了一个Web服务。使用SoapUI进行测试时,它工作正常。我写了一个Java客户端来调用webservice,它失败并出现以下错误:
org.apache.cxf.interceptor.Fault:无法识别消息部分客户。 (它是否存在于服务WSDL中?)
客户端程序从xml文件中读取数据,并将其作为Document添加到SOAPBody。
这是SoapUI中的输入数据:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:app="ApplyLoanRequest">
<soapenv:Header/>
<soapenv:Body>
<app:customer>
<contact>
<!--1 to 2 repetitions:-->
<name part="first">J</name>
<name part="last">H</name>
<country>US</country>
</contact>
</app:customer>
</soapenv:Body>
</soapenv:Envelope>
以下是我的客户端在writeTo System.out
的控制台上生成的SOAP启动BankLoan客户端...
服务请求......
====================================
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:app="ApplyLoanRequest"><SOAP-ENV:Header/><SOAP-ENV:Body><customer>
<contact>
<!--1 to 2 repetitions:-->
<name part="first">J</name>
<name part="last">H</name>
<country>IN</country>
</contact>
</customer></SOAP-ENV:Body></SOAP-ENV:Envelope>
服务响应......
====================================
皂:故障 faultcode = soap:客户端
不同之处在于,在SoapUI中,根元素与app:customer一起发送,而在我的客户端程序中没有前缀。这会导致错误吗?如何使用app:?
为root元素添加前缀这是Java客户端程序:
public class BankLoanClient
{
public Document convertXMLFileToDocument(String pathToFile) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(pathToFile);
return doc;
}
public static void main(String[] args) throws Exception
{
System.out.println("Starting BankLoan Client ... ");
SOAPMessage message = MessageFactory.newInstance().createMessage();
SOAPBody body = message.getSOAPBody();
SOAPPart part = message.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
envelope.addNamespaceDeclaration("app", "ApplyLoanRequest");
body.addDocument(new BankLoanClient().convertXMLFileToDocument(args[0]));
System.out.println("Service request ... ");
System.out.println("====================================");
message.writeTo(System.out);
System.out.println();
URL endpoint = new URL("http://localhost:8080/bankloan/services/applyloan");
SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = factory.createConnection();
SOAPMessage response = connection.call(message, endpoint);
connection.close();
// Process the response message
SOAPBody respBody = response.getSOAPBody();
Iterator it = respBody.getChildElements();
SOAPBodyElement element = (SOAPBodyElement) it.next();
System.out.println("Service response ... ");
System.out.println("====================================");
System.out.println(element.getNodeName());
it = element.getChildElements();
SOAPElement ret = (SOAPElement) it.next();
System.out.println(ret.getNodeName() + " = " + ret.getTextContent());
}
}
即使我将URL更改为指向wsdl,我也会收到同样的错误。
URL endpoint = new URL("http://localhost:8080/bankloan/services/applyloan?wsdl");