创立JAVA WebService步骤(3)

三、编写Dynamic Proxy方式访问服务

1、编写部署服务端程序,同上边DII方式,本次仍使用上边部署的HelloClient

2、编写代理接口


public interface HelloClientInterface
extends Java.rmi.Remote
{
      public String getName(String name)
          throws java.rmi.RemoteException;
}

3、编写并执行客户端程序TestHelloClient.java

import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import java.net.URL;
import javax.xml.namespace.QName;

public class TestHelloClient
{
      public static void main(String[] args)
          {
          try
          {
              String wsdlUrl =
                          "http://localhost:8080/Axis/HelloClient.jws?wsdl";
              String nameSpaceUri =
                          "http://localhost:8080/Axis/HelloClient.jws";
              String serviceName = "HelloClientService";
              String portName = "HelloClient";

ServiceFactory serviceFactory =
                          ServiceFactory.newInstance();
              Service afService =
                          serviceFactory.createService(new URL(wsdlUrl),
           new QName(nameSpaceUri, serviceName));
              HelloClientInterface proxy = (HelloClientInterface)
          afService.getPort(new QName(
                      nameSpaceUri, portName),
                                          HelloClientInterface.class);
              System.out.println
                          ("return value is "+proxy.getName("john") ) ;
          }catch(Exception ex)
          {
              ex.printStackTrace() ;
          }
      }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwpffj.html