微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WEBSERVICE 客户端实现2

实例2.HelloService

(1)首先定义服务方法代码如下所示:

[java]  view plain copy
  1. package edu.sjtu.webservice;  
  2.   
  3. public class HelloService {  
  4.     public String sayHelloNew() {  
  5.         return "hello";  
  6.     }  
  7.   
  8. public String sayHelloToPersonNew(String name) {  
  9. if (name == null) {  
  10.             name = "nobody";  
  11.         }  
  12.         return "hello," + name;  
  13.     }  
  14.     void updateData(String data) {  
  15.         System.out.println(data + " 已更新。");  
  16. }  
(2)参考实例1将这个方法发布为服务。

(3)编写客户端代码调用WebService(主要参考[5])

本文例子与其他例子最大的不同就在这里,其他例子一般需要根据刚才的服务wsdl生成客户端stub,然后通过stub来调用服务,这种方式显得比较单一,客户端必须需要stub存根才能够访问服务,很不方面。本例子的客户端不采用stub方式,而是一种实现通用的调用方式,不需要任何客户端存根即可访问服务。只需要指定对于的web servce地址、操作名、参数和函数返回类型即可。代码如下所示:

HelloServiceTest2.java

copy
    package edu.sjtu.webservice.test;  
  1. import javax.xml.namespace.QName;  
  2. import org.apache.axis2.AxisFault;  
  3. import org.apache.axis2.addressing.EndpointReference;  
  4. import org.apache.axis2.client.Options;  
  5. import org.apache.axis2.rpc.client.RPCServiceClient;  
  6. class HelloServiceTest2 {  
  7. private RPCServiceClient serviceClient;  
  8. private Options options;  
  9. private EndpointReference targetEPR;  
  10. public HelloServiceTest2(String endpoint) throws AxisFault {  
  11.         serviceClient = new RPCServiceClient();  
  12.         options = serviceClient.getoptions();  
  13.         targetEPR = new EndpointReference(endpoint);  
  14.         options.setTo(targetEPR);  
  15. public Object[] invokeOp(String targetNamespace, String opName,  
  16.             Object[] opArgs, Class<?>[] opReturnType) throws AxisFault,  
  17.             ClassNotFoundException {  
  18.         // 设定操作的名称  
  19.         QName opQName = new QName(targetNamespace, opName);  
  20. // 设定返回值  
  21.         // Class<?>[] opReturn = new Class[] { opReturnType };  
  22. // 操作需要传入的参数已经在参数中给定,这里直接传入方法调用  
  23. return serviceClient.invokeBlocking(opQName, opArgs, opReturnType);  
  24.     /** 
  25.      * @param args 
  26.      * @throws AxisFault 
  27.      * @throws ClassNotFoundException 
  28.      */  
  29. static void main(String[] args)              ClassNotFoundException {  
  30. // Todo Auto-generated method stub  
  31. final String endPointReference = "http://localhost:8080/WebServiceTest1/services/HelloService";  
  32. final String targetNamespace = "http://webservice.sjtu.edu";  
  33.         HelloServiceTest2 client = new HelloServiceTest2(endPointReference);  
  34.         String opName = "sayHelloToPersonNew";  
  35.         Object[] opArgs = new Object[] { "My Friends" };  
  36.         Class<?>[] opReturnType = new Class[] { String[].class };  
  37.         Object[] response = client.invokeOp(targetNamespace, opName,  
  38.                 opReturnType);  
  39.         System.out.println(((String[]) response[0])[0]);  
  40. 运行该程序,点击Run As->Java application,可以看到控制台端口的输出是:Hello,My Friends,表明客户端调用成功。该例子最大的不同和优势表现在客户端的调用方式,或者说是发起服务调用的方式,虽然比起客户端stub存根的方式,代码稍多,但是这种方式统一,不需要生产stub存根代码解决了客户端有很多类的问题。如果读者对这些代码进一步封装,我想调用方式很简单,只需要传递相关参数,这更好地说明了服务调用的优势。而且这种方式更加简单明了,一看便知具体含义。而不需要弄得stub类的一些机制。

    (4)改写客户端调用服务的代码

    (3)中提到的客户端应用代码写的略微有些繁杂,下面将上面的客户端调用service程序进行改写,简洁了许多。代码如下:

    HelloServiceTest.java

    copy

    import org.apache.axis2.AxisFault;  
  1. import org.apache.axis2.addressing.EndpointReference;  
  2. import org.apache.axis2.client.Options;  
  3. import org.apache.axis2.rpc.client.RPCServiceClient;  
  4. class HelloServiceTest {  
  5. void main(String args[]) throws AxisFault {  
  6. // 使用RPC方式调用WebService  
  7.         RPCServiceClient serviceClient =          Options options = serviceClient.getoptions();  
  8. // 指定调用WebService的URL  
  9.         EndpointReference targetEPR = new EndpointReference("http://localhost:8080/WebServiceTest1/services/HelloService");  
  10.         options.setTo(targetEPR);  
  11.           
  12. // 指定要调用的sayHelloToPerson方法及WSDL文件的命名空间  
  13.         QName opAddEntry = new QName("http://webservice.sjtu.edu","sayHelloToPersonNew");  
  14. // 指定sayHelloToPerson方法的参数值  
  15.         Object[] opAddEntryArgs = new Object[] { "xuwei" };  
  16. // 指定sayHelloToPerson方法返回值的数据类型的Class对象  
  17.         Class[] classes = new Class[] { String.// 调用sayHelloToPerson方法输出方法的返回值  
  18.         System.out.println(serviceClient.invokeBlocking(opAddEntry,op

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐