wsdl4j解析webservice的wsdl-根據webservice地址查詢介面方法名稱
最近研究了關於webservice的解析wsdl的問題,希望對各位有些幫助;
import java.util.Iterator;
import java.util.List;import java.util.Map;
import javax.wsdl.Binding;
import javax.wsdl.Definition;
import javax.wsdl.Input;
import javax.wsdl.Message;
import javax.wsdl.Operation;
import javax.wsdl.Part;
import javax.wsdl.Port;
import javax.wsdl.PortType;
import javax.wsdl.Service;
import javax.wsdl.WSDLException;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;
import javax.xml.namespace.QName;
public class TestDemo4 {
public static void main(String[]args)
{
String webServiceUrl = "http://localhost/Service1.asmx";
try{
WSDLFactory factory=WSDLFactory.newInstance();
WSDLReader reader=factory.newWSDLReader();
reader.setFeature("javax.wsdl.verbose",true);
reader.setFeature("javax.wsdl.importDocuments",true);
Definition def=reader.readWSDL(webServiceUrl+"?wsdl");
String namespaceURI=def.getTargetNamespace();
String localPart = webServiceUrl.split("/")[webServiceUrl.split("/").length-1];
if(localPart.contains(".")){
localPart = localPart.split("\\.")[0];
}
Service service =def.getService(new QName(namespaceURI,localPart));
String portStr = "";
Map<?, ?> ports = service.getPorts();
Iterator<?> itor = ports.entrySet().iterator();
while(itor.hasNext()){
Map.Entry<?, ?> map = (Map.Entry<?, ?>)itor.next();
Object key = map.getKey();
portStr = (key == null ?"":key.toString());
break;
}
Port port =service.getPort(portStr);
Binding binding=port.getBinding();
PortType portType=binding.getPortType();
List<?> operations=portType.getOperations();
Iterator<?> operIter=operations.iterator();
while(operIter.hasNext())
{
Operation operation=(Operation)operIter.next();
if(!operation.isUndefined())
{
System.out.println("方法名稱:"+operation.getName()) ;
}
}
}catch(WSDLException e)
{
e.printStackTrace();
}
}
}