webservice——SOAP简单开发

首先下载soap,把soap.war放到tomcat/webapp/目录下,在classpath下面加入soap.jar、mail.jar和 activation.jar,然后创建一个Service和Client,把Service.class放到 tomcat/webapp/soap/WEB-INF/classes目录下,然后把mail.jar和activation.jar放到 tomcat/webapp/soap/WEB-INF/lib下面,并写一个配置文件用于部署服务。

<isd:service
   xmlns:isd="http://xml.apache.org/xml-soap/deployment"
   id="urn:service" checkMustUnderstands="true">
      <isd:provider type="java" scope="Request" methods="setAlarm">
      <isd:java class="work.Service" static="false"/>
   </isd:provider>
</isd:service>


上面的work.Service是类的全名,setAlarm是提供的服务名,urn:service是URI。再写一个脚本来调用配置文件部署服务

java org.apache.soap.server.ServiceManagerClient
http://localhost:8080/soap/servlet/rpcrouter deploy deploy.xml

启动Tomcat之后,启动脚本,然后执行Client代码就可以了,Client的主体代码很简单,代码里面就不加入注释了。
  
    Call call = new Call ();
    call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
    call.setTargetObjectURI ("urn:service");
    call.setMethodName ("setAlarm");
    Parameter param = new Parameter("alarm", String.class, alarm, Constants.NS_URI_SOAP_ENC);
    Vector paramList = new Vector();
    paramList.addElement(param);
    call.setParams (paramList);
    URL url = new URL (" http://localhost:8080/soap/servlet/rpcrouter");
    Response resp = call.invoke (url, "");
    if (!resp.generatedFault()) {
      // Extract Return value
      Parameter result = resp.getReturnValue ();
      String greeting = (String) result.getValue();
      return greeting;
    }
    else {
      //  Extract Fault Code and String
      Fault f = resp.getFault();
      String faultCode = f.getFaultCode();
      String faultString = f.getFaultString();
      System.err.println("Fault Occurred (details follow):");
      System.err.println("Fault Code:  "+faultCode);
      System.err.println("Fault String:  "+faultString);
      return new String ("Fault Occurred.  No greeting for you!");
    }

你可能感兴趣的:(webservice——SOAP简单开发)