总结java开发web service的方法

总结java开发web service的方法

 

 
目前看到的有三种比较简单的方法:
1使用静态的stub
通过wsdl2java工具,处理相应ws的wsdl文件,我们可以得到远程ws的stub 直接调用这
个stub即可
AXIS提供的wsdl2java工具,如下:
java org.apache.axis.wsdl.WSDL2Java (WSDL-file-URL)
我们直接调用stub即可
eclipse也有相应的插件可以直接import wsdl来产生stub,如果你安装了EMF all in one版本的eclipse 3.1,就可以在视图里把Web Service的相关视图打开,接着就可以通过Web Service的Wizard来创建出相关的Web Service Client或者Server

 

2 Dynamic Proxy
根据远程wsdl,利用javax.xml.rpc.Service的getPort函数,可以得到远程ws的一个 D
ynamic Proxy
编写代理接口

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

客户端程序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(http://www.zhmy.com/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() ;
        }
    }
}
 

3 DII
Dynamic Invocation Interface 这个最好理解,比如你动态获得了一个类,只知道类的
名字,你要调用他的一个方法,只好使用reflection得到你要调用的类,相应的参数信
息,然后调用
使用DII调用WS的时候,你知道的只是一个WSDL的地址,通过解析wsdl,你可以得到相应
的ws endpoint的信息,然后通过javax.xml.rpc.Call的setOperationName, addParam
eter等函数来指定要调用的函数,指定参数,然后调用
提供DII调用的原因是,我们有可能使用程序自动的去动态调用网络上的WS,而这个WS的
一切信息都是来自其WSDL
,只有通过DII,我们才有可能动态的去调用这个ws
例子:
服务器端程序

public class HelloClient
{
    public String getName(String name)
    {
        return "hello "+name;
    }
}
把源码拷贝到AXIS_HOME下,并改名为 HelloClient.jws

客户端程序
import org.apache.Axis.client.Call;
import org.apache.Axis.client.Service;
import Javax.xml.namespace.QName;
import Javax.xml.rpc.ServiceException;
import Java.net.MalformedURLException;
import Java.rmi.RemoteException;

public class SayHelloClient2
{
    public static void main(String[] args)
        {
        try
                {
  String endpoint =  "http://localhost:8080/axis/HelloClient.jws";

    Service service = new Service();
            Call call = null;

            call = (Call) service.createCall();

            call.setOperationName
   (new QName("http://localhost:8080/axis/HelloClient.jws", "getName"));
            call.setTargetEndpointAddress(new Java.net.url(http://www.zhmy.com/endpoint));

 String ret = 
 (String) call.invoke(new Object[] 
     {"zhangsan"});
  System.out.println
  ("return value is " + ret);
        } 
                catch (Exception ex)
                {
       ex.printStackTrace();
        }
    }
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值