一、首先创建webservice客户端项目webservice_client
1、目录
[img]http://shawnfree.iteye.com/upload/attachment/97024/edc81dc1-ae38-3100-85be-c9856fbbb3f4.bmp[/img]
2、将所需jar包括到lib项目lib目录下
二、开发webservice客户端
1、开发客户端代码。如下:
com.smartdot.webservice.WebserviceClient
2、编写字符串转换成OMElement对象公共类,
com.smartdot.util.OMElementUtils
[color=red][size=large]到此为止,webservice客户端已经开发完毕,以上代码注释均是自己理解的,如果有什么不对的地方请指教。谢谢!!![/size]![/color]
1、目录
[img]http://shawnfree.iteye.com/upload/attachment/97024/edc81dc1-ae38-3100-85be-c9856fbbb3f4.bmp[/img]
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;
}
}
}
[color=red][size=large]到此为止,webservice客户端已经开发完毕,以上代码注释均是自己理解的,如果有什么不对的地方请指教。谢谢!!![/size]![/color]