`

webservice学习二之(2)axis2客户端方式开发总结(附件含有项目)

阅读更多
一、首先创建webservice客户端项目webservice_client
   1、目录
    
    2、将所需jar包括到lib项目lib目录下
二、开发webservice客户端
   1、开发客户端代码。如下:
    com.smartdot.webservice.WebserviceClient
/**
 *
 *  This is a part of the smartdot cpms system.
 *  Copyright (C) 2008-2009 Smartdot Corporation
 *  All rights reserved.
 *
 *  Licensed under the Smartdot private License.
 *  Created on 2009-4-22
 *  @author YangLin
**/

package com.smartdot.webservice;


import java.util.Iterator;

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.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

import com.smartdot.util.OMElementUtils;

/**
 * webservice客户端
 * @author YangLin
 *
 */
public class WebserviceClient {

	public static void main(String[] args) throws Exception {
		/*****targetEPR指定打包的Service(.aar文件)在容器中的物理位置*******/
		EndpointReference targetEPR = new EndpointReference(
		"http://localhost:8080/webservice_portal/services/APIWebService");
		try{
			//生成一个options对象然后把前面的地址放进去。
			  Options options = new Options();
			  options.setTo(targetEPR);
			  ServiceClient sender = new ServiceClient();
		      sender.setOptions(options);
		      System.out.println("webservice start");
		      OMElement sayHello = doSetOMElement();
		      OMElement result = sender.sendReceive(sayHello);
		      System.out.println("webservice end");
		      //到目前为止,已经将客户端信息发送到服务器端,并且接收webservice服务器返回的值。
		      System.out.println(result);
		      /**
		       * 分两种方式解析OMElement对象,并且当前对象格式必须是<Z><A>b</A><B>b</B></Z>
		       * 如果不是这样格式,则用result.getFirstElement(),进行提取,知道是上述格式。
		       */

		      OMElement firstElement=result.getFirstElement();
		      System.out.println("firstElement="+firstElement);
		      //第二种方式直接查找匹配子节点name属性
		      OMElement childElement=firstElement.getFirstChildWithName(new QName("state"));
		      System.out.println("第二种方式="+childElement.getText());
		     
		      //第一种方式遍历子节点对象OMElement,集合Iterator存放的内容是<A>a</A><B>b</B>
		      Iterator  it=firstElement.getChildElements();
		      while(it.hasNext()){
		    	  OMElement childs=(OMElement)it.next();
		    	  if(childs.getLocalName().equals("state")){
		    		  System.out.println("第一种方式="+childs.getText());
		    	  }
		      }
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 *创建调用webservice接口所需的参数OMElement对象。
	 * @return
	 */
	public static OMElement doSetOMElement(){
		/************创建调用webservice接口所需的参数OMElement对象*************/
		 OMFactory fac=OMAbstractFactory.getOMFactory();
		 OMNamespace omNs=fac.createOMNamespace("", "");
		//method中包含了需要调用的web service的方法名称,其中updatePeople就是其中的一个方法
		 OMElement method=fac.createOMElement("receiveUser",omNs);
		 String xmlInfo = "<people><name>yanglin</name><password>smartdot</password></people>";
		 OMElement content=new OMElementUtils().toOMElement(xmlInfo,"utf-8");
		 method.addChild(content);
		 return method;	
	}
}


2、编写字符串转换成OMElement对象公共类,
com.smartdot.util.OMElementUtils
/**
 *
 *  This is a part of the smartdot cpms system.
 *  Copyright (C) 2008-2009 Smartdot Corporation
 *  All rights reserved.
 *
 *  Licensed under the Smartdot private License.
 *  Created on 2009-4-22
 *  @author YangLin
 **/

package com.smartdot.util;

import java.io.ByteArrayInputStream;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;

public class OMElementUtils {

	// 将xml字符创解析成OMElement对象。
	public  OMElement toOMElement(String xmlStr, String encoding) {
		OMElement xmlValue;
		try {
			xmlValue = new StAXOMBuilder(new ByteArrayInputStream(xmlStr
					.getBytes(encoding))).getDocumentElement();
			return xmlValue;
		} catch (Exception e) {
			return null;
		}
	}
}


到此为止,webservice客户端已经开发完毕,以上代码注释均是自己理解的,如果有什么不对的地方请指教。谢谢!!!
  • 大小: 3 KB
分享到:
评论
4 楼 lovin_fang 2012-04-03  
非常感谢楼主啊~~~
3 楼 波斯鼠 2011-12-08  
太感谢了,正好学习学习
2 楼 hngslifeng 2010-11-12  
非常好,谢谢
1 楼 jelver 2010-01-28  
写得挺详细,这么多人看了,都不回个贴,太不厚道了

相关推荐

Global site tag (gtag.js) - Google Analytics