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

本文介绍了一个具体的Webservice客户端项目的创建过程,包括项目目录结构、所需jar包的引入及客户端代码的编写。通过示例展示了如何使用Apache Axis2库实现与Webservice服务器的交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、首先创建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

/**
*
* 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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值