WebService学习之路

最近工作中用到了webService,之前也稍微学习了一点,不过从来没用过所以忘的差不多了,这一次主要是以应用为主。

主要用的是Axis2的方式进行webservice应用的,主要有三种方式

第一RPC方式,不生成客户端代码

第二,document方式,不生成客户端代码

第三,用wsdl2java工具,生成客户端方式调用

我个人调用webService的方式主要是第三种,生成两个java文件后,在项目中引入依赖便可直接当作java使用。

主要步骤为下载axis2-1.7.9-bin.zip客户端

解压缩后,切换到bin目录下

然后打开cmd,输入命令

wsdl2java -uri C:\Users\13920\Desktop\WebService\webservice服务器测试程序\PatrolSystem.wsdl -p com.patrol.test -o e:\test

-uri代表wsdl文件的路径,或者一个链接

-p代表生成的java文件所在包路径,可自己定义

-0代表生成的文件的存储路径

成功后会生成build.xml和src两个文件,src内含有包和生成的java类

调用java的方式

	try {
		  PatrolSystemStub patrolSystemStub = new PatrolSystemStub();
		  PatrolSystemStub.GetRecentPatrolEvent ad= new PatrolSystemStub.GetRecentPatrolEvent();
		  PatrolSystemStub.GetPatrolBayInfo bayInfo= new PatrolSystemStub.GetPatrolBayInfo();
		  System.out.println(patrolSystemStub.getPatrolBayInfo(bayInfo).getBayInfos());	    
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
	    }

下面的demo主要来自于https://blog.csdn.net/zhangmeng_07/article/details/54694277


package samples.quickstart.client;  
  
import javax.xml.namespace.QName;  
import org.apache.axiom.om.OMAbstractFactory;  
import org.apache.axiom.om.OMElement;  
import org.apache.axiom.om.OMFactory;  
import org.apache.axiom.om.OMNamespace;  
import org.apache.axis2.AxisFault;  
import org.apache.axis2.addressing.EndpointReference;  
import org.apache.axis2.client.Options;  
import org.apache.axis2.client.ServiceClient;  
import org.apache.axis2.rpc.client.RPCServiceClient;  
import samples.quickstart.StockQuoteServiceStub;  
import samples.quickstart.xsd.GetPrice;  
import samples.quickstart.xsd.GetPriceResponse;  
  
public class StockQuoteClient {  
  
  /** 
   * 方法一: 
   * 应用rpc的方式调用 这种方式就等于远程调用, 
   * 即通过url定位告诉远程服务器,告知方法名称,参数等, 调用远程服务,得到结果。 
   * 使用 org.apache.axis2.rpc.client.RPCServiceClient类调用WebService 
   * 
    【注】: 
     
        如果被调用的WebService方法有返回值 应使用 invokeBlocking 方法 该方法有三个参数 
          第一个参数的类型是QName对象,表示要调用的方法名; 
          第二个参数表示要调用的WebService方法的参数值,参数类型为Object[]; 
            当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。 
          第三个参数表示WebService方法的 返回值类型的Class对象,参数类型为Class[]。 
         
         
        如果被调用的WebService方法没有返回值 应使用 invokeRobust 方法 
          该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同。 
 
        在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名, 
        也就是 <wsdl:definitions>元素的targetNamespace属性值。 
   * 
   */  
  public static void testRPCClient() {  
    try {  
      // axis1 服务端  
// String url = "http://localhost:8080/StockQuote/services/StockQuoteServiceSOAP11port?wsdl";  
      // axis2 服务端  
      String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService?wsdl";  
  
      // 使用RPC方式调用WebService  
      RPCServiceClient serviceClient = new RPCServiceClient();  
      // 指定调用WebService的URL  
      EndpointReference targetEPR = new EndpointReference(url);  
      Options options = serviceClient.getOptions();  
      //确定目标服务地址  
      options.setTo(targetEPR);  
      //确定调用方法  
      options.setAction("urn:getPrice");  
  
      /** 
       * 指定要调用的getPrice方法及WSDL文件的命名空间 
       * 如果 webservice 服务端由axis2编写 
       * 命名空间 不一致导致的问题 
       * org.apache.axis2.AxisFault: java.lang.RuntimeException: Unexpected subelement arg0 
       */  
      QName qname = new QName("http://quickstart.samples/xsd", "getPrice");  
      // 指定getPrice方法的参数值  
      Object[] parameters = new Object[] { "13" };  
        
      // 指定getPrice方法返回值的数据类型的Class对象  
      Class[] returnTypes = new Class[] { double.class };  
  
      // 调用方法一 传递参数,调用服务,获取服务返回结果集  
      OMElement element = serviceClient.invokeBlocking(qname, parameters);  
      //值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。  
      //我们可以对之灵活应用,下面我取第一个元素值,并打印之。因为调用的方法返回一个结果  
      String result = element.getFirstElement().getText();  
      System.out.println(result);  
  
      // 调用方法二 getPrice方法并输出该方法的返回值  
      Object[] response = serviceClient.invokeBlocking(qname, parameters, returnTypes);  
      // String r = (String) response[0];  
      Double r = (Double) response[0];  
      System.out.println(r);  
  
    } catch (AxisFault e) {  
      e.printStackTrace();  
    }  
  }  
  
  /** 
   * 方法二: 应用document方式调用(构建请求报文)
   * 用ducument方式应用现对繁琐而灵活。现在用的比较多。因为真正摆脱了我们不想要的耦合 
   */  
  public static void testDocument() {  
    try {  
      // String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService";  
      String url = "http://localhost:8080/StockQuote/services/StockQuoteServiceSOAP11port?wsdl";  
  
      Options options = new Options();  
      // 指定调用WebService的URL  
      EndpointReference targetEPR = new EndpointReference(url);  
      options.setTo(targetEPR);  
      // options.setAction("urn:getPrice");  
  
      ServiceClient sender = new ServiceClient();  
      sender.setOptions(options);  
        
        
      OMFactory fac = OMAbstractFactory.getOMFactory();  
      String tns = "http://quickstart.samples/";  
      // 命名空间,有时命名空间不增加没事,不过最好加上,因为有时有事,你懂的 
      // 第一个参数命名空间地址,第二个参数是命名空间前缀 
      OMNamespace omNs = fac.createOMNamespace(tns, "");  
      // 第一个参数是构建标签名称,第二个参数是命名空间
      OMElement method = fac.createOMElement("getPrice", omNs);  
      OMElement symbol = fac.createOMElement("symbol", omNs);
      // 标签设值  
      symbol.setText("13");  
      //symbol.addChild(fac.createOMText(symbol, "Axis2 Echo String ")); 
      // 添加子标签 
      method.addChild(symbol);  
      // 构建报文
      method.build();  
      // 发送请求返回报文  
      OMElement result = sender.sendReceive(method);  
  
      System.out.println(result);  
  
    } catch (AxisFault axisFault) {  
      axisFault.printStackTrace();  
    }  
  }  
  
 /** 
  * 为SOAP Header构造验证信息, 
  * 如果你的服务端是没有验证的,那么你不用在Header中增加验证信息 
  * 
  * @param serviceClient 
  * @param tns 命名空间 
  * @param user 
  * @param passwrod 
  */  
  public void addValidation(ServiceClient serviceClient, String tns , String user, String passwrod) {  
    OMFactory fac = OMAbstractFactory.getOMFactory();  
    OMNamespace omNs = fac.createOMNamespace(tns, "nsl");  
    OMElement header = fac.createOMElement("AuthenticationToken", omNs);  
    OMElement ome_user = fac.createOMElement("Username", omNs);  
    OMElement ome_pass = fac.createOMElement("Password", omNs);  
      
    ome_user.setText(user);  
    ome_pass.setText(passwrod);  
      
    header.addChild(ome_user);  
    header.addChild(ome_pass);  
  
    serviceClient.addHeader(header);  
  }  
  
    
  /** 
   * 方法三:利用axis2插件生成客户端方式调用 
   * 
   */  
  public static void testCodeClient() {  
    try {  
      String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService";  
      StockQuoteServiceStub stub = new StockQuoteServiceStub(url);  
      GetPrice request = new GetPrice();  
      request.setSymbol("ABCD");  
      GetPriceResponse response = stub.getPrice(request);  
      System.out.println(response.get_return());  
    } catch (org.apache.axis2.AxisFault e) {  
      e.printStackTrace();  
    } catch (java.rmi.RemoteException e) {  
      e.printStackTrace();  
    }  
  
  }  
  
  public static void main(String[] args) {  
     StockQuoteClient.testRPCClient();  
// StockQuoteClient.testDocument();  
    // StockQuoteClient.testCodeClient();  
  
  }  
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值