JAVA 读取webservice接口

来源:互联网 发布:ubuntu 镜像 知乎 编辑:程序博客网 时间:2024/05/16 04:55

1. wsdl

<?xml version="1.0" encoding="utf-8" ?>
<wsdl:definitions xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://tempuri.org/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:import namespace="http://www.w3.org/2001/XMLSchema" />
<s:element name="GetIdPerinfo">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="in_id" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="GetIdPerinfoResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetIdPerinfoResult">
<s:complexType>
<s:sequence>
<s:element ref="s:schema" />
<s:any />
</s:sequence>
</s:complexType>
</s:element>
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message name="GetIdPerinfoSoapIn">
<wsdl:part name="parameters" element="tns:GetIdPerinfo" />
</wsdl:message>
<wsdl:message name="GetIdPerinfoSoapOut">
<wsdl:part name="parameters" element="tns:GetIdPerinfoResponse" />
</wsdl:message>
<wsdl:portType name="HRODBShareSoap">
<wsdl:operation name="GetIdPerinfo">
<documentation xmlns="http://schemas.xmlsoap.org/wsdl/">按指定的ID得到基本信息</documentation>
<wsdl:input message="tns:GetIdPerinfoSoapIn" />
<wsdl:output message="tns:GetIdPerinfoSoapOut" />
</wsdl:operation>

</wsdl:portType>
<wsdl:binding name="HRODBShareSoap" type="tns:HRODBShareSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
<wsdl:operation name="GetIdPerinfo">
<soap:operation soapAction="http://tempuri.org/GetIdPerinfo" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>

</wsdl:binding>
<wsdl:service name="HRODBShare">
</wsdl:definitions>


2. Java 代码

package mytest;
import java.io.ByteArrayInputStream;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.types.Schema;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class CallWS {

 public String invoke(Object [] obj ) throws Exception
 {
    
  String endpointURL = "http://10.3.6.53/hroweb/hrodbshare.asmx";  
  String namespaceURI = "http://tempuri.org/" ;//命名空间
  String soapactionURI = "http://tempuri.org/GetIdPerinfo"; //soapactionURI
  String remotemethod = "GetIdPerinfo";//方法名
  
  String id="147493";
  
  Service  service = new Service();
  
  Call call=(Call) service.createCall();
  
  call.addParameter(new QName(namespaceURI,"in_id"),org.apache.axis.encoding.XMLType.XSD_STRING ,javax.xml.rpc.ParameterMode.IN);
  call.setReturnType(org.apache.axis.encoding.XMLType.XSD_SCHEMA);

  
  call.setUseSOAPAction(true);
     call.setSOAPActionURI(soapactionURI);
  
  call.setTargetEndpointAddress(new java.net.URL(endpointURL).toString() );
  
     QName  qname = new QName(namespaceURI, remotemethod);
    
     call.setOperationName(qname);
    
        Schema schema = (Schema) call.invoke(obj);
    
     String res = schema.get_any()[1].getAsString();
   
  return res;
 }
 
 
 public static void main(String [] args) throws Exception
 {
  CallWS ll = new CallWS();
  try
  {
   String s = ll.invoke(new Object[]{"147493"});
   
   SAXReader reader = new SAXReader();
   Document doc = reader.read(new ByteArrayInputStream(s.getBytes()));
   
   Node node = (Node) doc.selectSingleNode("/diffgr:diffgram/NewDataSet/Table/NOTES_EMAIL");
   
   System.out.println("getStringValue(): "+node.getStringValue());//
   System.out.println("getText(): "+node.getText());//node.getStringValue() = node.getText()
  }
  catch (MalformedURLException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  catch (RemoteException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  catch (ServiceException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
}

原创粉丝点击