Axis2 发布webservice

  工作中需要使用webservice了,但是对于刚刚参加工作的新人来说,我是一直“久仰”其大名,但是一次都没用过。经过了曲折的过程,一切困难都已经是过去式,下面将我学习的经验和大家分享,如果有不正确的地方,欢迎各位大牛指正。

 

1.        下载axis2的架包

 对于axis2我就不多讲了(不知道自己查找百度百科),下面直接给出链接地址

http://axis.apache.org/axis2/java/core/download.cgi


  下载 wardistribution 这个对应的包,具体怎么使用后面在详细说。

 

  2 项目框架的搭建(用词不是很准确,直接看正文吧)

   在学习的过程中,发现很多的博客都是直接发布axis2的webservice的,在一个已有的项目中发布webservice讲述的不是特别清楚,在不断的查找中终于是有所收获,和大家分享一下,同时感谢这篇博客的原创者。

   http://wangronaldo.iteye.com/blog/1456441

  下面把我自己的经验分享给大家。

  

   首先,建一个web项目,例如本人的HelloAxis2。

   然后,将第一步下载的架包解压开(第一次解压后,有一个axis2.war,接着再解压一次),将conf、lib、modules放入WEB-INF文件中,其中lib是axis2需要的架包(中间可能有一些不需要的,这个如果谁有详细的研究,欢迎分享)

   最后,新建一个services文件夹,然后在services文件下新建一个文件夹(任意取名),再新建META-INF文件夹,最后再新增services.xml,接口信息就写在这里面。

具体路径:WEB-INF/services/myservice/META-INF/services.xml

 

项目整体框架如图

 

   注:我在项目开发的过程中发现conf这个文件的名称是能改的,如改为config,不确定是否具有局限性,大家可以在自己的项目中尝试一下,反正不浪费多少时间。

 

3 服务类代码编写及配置文件的配置

 

   服务类的代码

 

   就这两句代码,为了省事直接截图了,大家自己敲吧:)

 

 

配置web.xml

将一下配置添加进去


   <servlet>
	<servlet-name>AxisServlet</servlet-name>
	<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
 </servlet>
 <servlet-mapping>
	<servlet-name>AxisServlet</servlet-name>
	<url-pattern>/services/*</url-pattern>
 </servlet-mapping>

配置services.xml

<?xml version="1.0" encoding="UTF-8"?>
<!—服务名称--
<service name="Hello_axis2" >
<description>
this is  a aixs2 testProgram
</description>
--类--
<parameter name="ServiceClass">com.Hello_axis2</parameter>
--方法名--
<operation name="sayChinese">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>

<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>

</service>
<p>启动tomcat,在浏览器输入:</p><p><a target=_blank href="http://localhost:8080/HelloAxis2/services/Hello_axis2?wsdl">http://localhost:8080/HelloAxis2/services/Hello_axis2?wsdl</a></p><p>出现下图所示的页面就表示成功了,红色框为比较重要的信息,在webservice客户端编码时需要用到。</p><p></p><p>
</p>

启动tomcat,在浏览器输入:

http://localhost:8080/HelloAxis2/services/Hello_axis2?wsdl

出现下图所示的页面就表示成功了,红色框为比较重要的信息,在webservice客户端编码时需要用到。

 

4 客户端测试代码编写

  新建一个web项目,将service端的lib和conf拷贝到新项目中,项目结构如图

  

客户端的代码是在网上直接找到的,不知道是哪位大牛写的,直接伸手拿来用了。

package com;
import java.util.Iterator;
import javax.xml.namespace.QName;
import org.apache.axiom.om.OMElement;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class Axis2CommonClient {
	public Axis2CommonClient() {

	}
	/**
	* 
	* @param srvcUrl
	*            访问的url
	* @param namespace
	*            命名空间
	* @param operateName
	*            你想调用的方法
	* @param param
	*            要传入的参数。
	* @return
	* @throws Exception
	*/
	public Object excute(String srvcUrl, String namespace, String operateName,
	Object param[]) throws Exception {
		//namespce : http://com operateName :方法名
	QName qname = new QName(namespace, operateName);
	RPCServiceClient client = new RPCServiceClient();
	Options options = new Options();
	options.setTo(new EndpointReference(srvcUrl));
	options.setAction("urn:" + operateName);
	client.setOptions(options);
	OMElement element = client.invokeBlocking(qname, param);
	if (element != null && !"".equals(element)) {
	Iterator<?> values = element.getChildrenWithName(new QName(
	namespace, "return"));
	while (values.hasNext()) {
	OMElement omElement = (OMElement) values.next();
	return omElement.getText();
	}
	} else {
	return element;
	}
	return "-1";

	}
}


Client 代码
  package com;

public class TestClient1 {
	public static void main(String[] args) throws Exception {

		String url = "http://localhost:8080/HelloAxis2/services/Hello_axis2";

		String namespace = "http://com";//此处的命名空间 example 应与发布的服务中的命名空间保持一致.

		String operateName = "tras";

		String[] param = new String[] { "xx" };

		Axis2CommonClient axClient = new Axis2CommonClient();

		//Object obj = axClient.excute(url, namespace, operateName, param);

		//System.out.println(obj);

		operateName = "sayChinese";

		Object obj = axClient.excute(url, namespace, operateName,param);

		System.out.println(obj);

		}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值