服务端的webservices的发布是基于jax-ws规范发布的。下面是客户端动态调用的代码,调用的web服务是可配置的。
1:在xml文档里配置webservices的wsdl地址,端口名和操作名
2:提供一个方法读取xml文档,返回一个List
List list= ReadAndWriteXML.getWebServiceWsdlList();
if(list!=null){
String responseContent ="";
for(int i=0;i<list.size();i++){
Address address = (Address)list.get(i);
try {
// Define the service.
QName svcQname = new QName(
address.getTargetNameSpace(),
address.getServiceName());
QName portQName = new QName(
address.getTargetNameSpace(),
address.getPortName());
Service svc = Service.create(svcQname);
svc.addPort(
portQName,
SOAPBinding.SOAP11HTTP_BINDING,
address.getSoapAddress());
// Create the dynamic invocation object from this service.
Dispatch<Source> dispatch = svc.createDispatch(
portQName,
Source.class,
Service.Mode.PAYLOAD);
// Build the message.
String content =
"<?xml version=/"1.0/" encoding=/"gb2312/"?>"+
"<soap:"+address.getOperationName()+" xmlns:soap=/""+address.getTargetNameSpace()+"/" "+
"soap:encodingStyle=/"http://www.w3.org/2001/12/soap-encoding/">"+
"<fullFormerName>"+fullFormerName+"</fullFormerName>"+
"<fullLaterName>"+fullLaterName+"</fullLaterName>"+
"</soap:"+address.getOperationName()+">";
ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes());
Source input = new StreamSource(bais);
// Invoke the operation.
Source output = dispatch.invoke(input);
// Process the response.
StreamResult result = new StreamResult(new ByteArrayOutputStream());
Transformer trans;
trans = TransformerFactory.newInstance().newTransformer();
trans.transform(output, result);
ByteArrayOutputStream baos = (ByteArrayOutputStream) result.getOutputStream();
// Write out the response content.
responseContent = new String(baos.toByteArray());
} catch (TransformerConfigurationException e) {
System.out.println("NounConsistencyAcceptor 92 Line TransformerConfigurationException ...");
} catch (TransformerFactoryConfigurationError e) {
System.out.println("NounConsistencyAcceptor 94 Line TransformerFactoryConfigurationError ...");
} catch (TransformerException e) {
System.out.println("NounConsistencyAcceptor 96 Line TransformerException ...");
}
}
}
xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 配置说明:targetNameSpace为wsdl文档中的targetNameSpace -->
<!-- serviceName为wsdl文档中的service片断 name名 -->
<!--portName为wsdl文档中的service片断 port name名 -->
<!--soapAddress为wsdl文档中的service片断 soap:address location地址 -->
<Message>
<address>
<targetNameSpace>http://service.zf.com/</targetNameSpace>
<serviceName>FileConsistServiceService</serviceName>
<portName>FileConsistServicePort</portName>
<operationName>consistDesignNodeName</operationName>
<soapAddress>http://127.0.0.1:8080/KMEntity/FileConsistService</soapAddress>
</address>
<address>
<targetNameSpace>http://service.zf.com/</targetNameSpace>
<serviceName>FileConsistServiceService</serviceName>
<portName>FileConsistServicePort</portName>
<operationName>consistDesignParaName</operationName>
<soapAddress>http://127.0.0.1:8080/KMEntity/FileConsistService</soapAddress>
</address>
<address>
<targetNameSpace>http://service.zf.com/</targetNameSpace>
<serviceName>IndexConsistServiceService</serviceName>
<portName>IndexConsistServicePort</portName>
<operationName>consistDesignNodeName</operationName>
<soapAddress>http://127.0.0.1:8080/KMEntity/IndexConsistService</soapAddress>
</address>
<address>
<targetNameSpace>http://service.zf.com/</targetNameSpace>
<serviceName>IndexConsistServiceService</serviceName>
<portName>IndexConsistServicePort</portName>
<operationName>consistDesignParaName</operationName>
<soapAddress>http://127.0.0.1:8080/KMEntity/IndexConsistService</soapAddress>
</address>
</Message>