---
title: "使用Axis2搭建web Service"
author: "yangmeng_3331"
date: "2014-06-06T09:59:57Z"
tags: []
categories: ["编程语言"]
slug: "axis2-web-service"
draft: false
lastmod: "2014-06-06T16:29:37Z"
id: 2076386
url: "https://yangmeng.iteye.com/blog/2076386"
---

1、首先下载需要的一些相关axis2.zip及jar包
   axis2：<http://axis.apache.org/axis2/java/core/download.cgi>。
   eclipse的官方提供的axis插件工具，可以打包aar文件和生成客户端调用代码:<http://axis.apache.org/axis2/java/core/tools/index.html>。
   下载其中的Service Archive Wizard - Eclipse Plug-in和Code Generator Wizard - Eclipse Plug-in(<span style="color:blue">本文也提供这两个jar下载</span>)。
2、配置axis2及eclipse
   2.1 eclipse中axis2环境配置：Window->Perferences->Web Services->Axis2 perferences->Axis2 Runtime->Axis2 Runtime location,Browse选择解压axis2-1.6.2-bin.zip得到的axis2-1.6.2文件目录。如图：
![](http://dl2.iteye.com/upload/attachment/0097/8138/ec926f68-1a2b-3019-8f9b-794b563126cb.png)
   2.2 安装插件：解压axis2-eclipse-codegen-plugin-1.6.2.zip和axis2-eclipse-service-plugin-1.6.2.zip，把得到的两个jar包放入eclipse目录下的\plugins\中，重启eclipse。
3、用eclipse的axis2插件发布web服务
   3.1 在eclipse中创建一个项目axis2,创建一个webservice服务器类,创建写完方法后编译一下

```java

package axis.test;

public class Axis2Test {

    public String axisTest(String name){       
        if(name==null){  
            name = "nobody";  
        }             
        return "hello,"+name;  
    }  
	
}

```

    3.2  在eclipse的空白workspace处，右键new->Other,在弹出的对话框中，找到Axis2 Service Archiver,双击->选择HelloWorldService所在项目的class路径，如图
![](http://dl2.iteye.com/upload/attachment/0097/8140/52247ea0-b920-359f-95e3-ec28ca56b41c.jpg)
    然后next,选中skip WSDL,继续next,在next选中Generate the service xml automatically继续next，然后输入Service name、Class name(类全路径名)点击load，在下面的Method name中出现之前写的方法名就表示成功了，继续点击next,如图：
![](http://dl2.iteye.com/upload/attachment/0097/8142/cbb53b42-1599-3d27-9368-225a233d3ba1.jpg)
    选择OutPut file location路径(项目的src根目录),点击Finish，如图：
![](http://dl2.iteye.com/upload/attachment/0097/8144/b35c8efb-15f9-346c-85b1-ad7cd951035f.jpg)
    刷新项目后就能在src根目录下看见my_service.aar包了，在WEB-INF下新建services文件夹，将my_service.aar复制到该文件夹下。
    将axis2项目下的WEB-INF下的conf文件夹复制到项目的WEB-INF下，修改里面的axis2.xml文件，将<module ref="addressing"/>这句注释掉。
    将axis2项目中axis2-web文件夹复制到WebContent文件夹下，秩序保留其中的listServices.jsp(<span style="color:red">将里面include的页面除了httpbase.jsp外都删掉</span>)及include文件夹下的httpbase.jsp。
    在web.xml配置中添加如下配置：

```java

    <servlet>
        <servlet-name>AxisServlet</servlet-name>
        <display-name>Apache-Axis Servlet</display-name>
        <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>/servlet/AxisServlet</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>*.jws</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

```

    发布项目，打开http://localhost:8080/axis2/services/listServices后可以看到之前写的webService类及方法了，点击服务名就可以看到wsdl内容了，如图：
![](http://dl2.iteye.com/upload/attachment/0097/8392/24722c26-ea87-34c3-99b6-f1504bb160a0.jpg)
![](http://dl2.iteye.com/upload/attachment/0097/8394/bb17bde0-e035-3213-86b9-3446105ad7d4.jpg)

**调用webService**

```java

package axis.test;

import javax.xml.namespace.QName;

import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;


public class Axis2Client {

	/**
	 * @param args
	 *@data 2014-6-6 上午11:34:37
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			//使用RPC方式调用WebService
			RPCServiceClient serviceClient = new RPCServiceClient(); 
			Options options = serviceClient.getOptions(); 
			//指定调用WebService的URL  
			EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/Axis2Test");  
			options.setTo(targetEPR);  
			//指定调用方法的参数值
			Object[] opAddEntryArgs = new Object[] {"成功了"};  
			//指定调用方法返回值的数据类型的Class对象
			Class[] classes = new Class[] {String.class};  
			//指定要调用的方法名称及WSDL文件的命名空间
			QName opAddEntry = new QName("http://test.axis", "axisTest");
			//调用并输出返回值
			System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

```