您的位置:首页 > 其它

Axis2 WebService的配置、发布、调用

2013-11-09 20:48 369 查看

准备工作

1. 下载 axis2-1.6.2-bin.zip,axis2-1.6.2-war.zip文件。http://axis.apache.org/axis2/java/core/download.cgi 

2. 环境变量配置 

AXIS2_HOME

JAVA_HOME

3. axis2-1.6.2-war.zip压缩文件中的axis2.war复制到 %TOMCAT-HOME%/webapps目录下,然后启动Tomcat

    访问 http://localhost:8080/axis2/ 看是否正常。



点击Services会进入service列表页面,当前只有一个Version服务。

4. 下载axis2-eclipse-codegen-wizard.zip和axis2-eclipse-service-archiver-wizard.zip 插件

     http://archive.apache.org/dist/ws/axis2/tools/1_4_1/ 

    解压后得到两个文件夹:Axis2_Codegen_Wizard_1.3.0和Axis2_Service_Archiver_1.3.0,拷贝至eclipse\dropins

             注另一种方式,测试不成功,不推荐。下载axis-eclipse-codegen-plugin-1.6.2.zip和axis-eclipse-service-plugin1.6.2.zip文件解压后将plugins复制到%ECLIPSE_HOME%/plugins/目录下。 http://mirrors.cnnic.cn/apache/axis/axis2/java/core/1.6.2/
    

5. 安装完成后,IDE中选择new -> other
有Axis2 Wizards,说明插件安装成功。

AXIS2发布Web Services

一. 工程文件

新建Axis2Service1  java工程;

新建/Axis2Service1/src/ws/TestWs.java文件;

package ws;

public class TestWs {

public String showName(String name){
return name;
}
public String getName(){
return "Axis2Service Sample";
}
}


二. aar部署方式

1. 手动打包

新建 /Axis2Service1/deploy文件夹,将/Axis2Service1/bin/目录下的class下文复制过来;

新建/Axis2Service1/deploy/META-INF/services.xml文件

<service name="AxisService">

<description>AxisService</description>
<parameter name="ServiceClass">ws.TestWs</parameter>
<operation name="showName">
<messageReceiver
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>

<operation name="getName">
<messageReceiver
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
</operation>
</service>


2. 插件打包

在IDE中选择New -> other -> Axis2 Service Archiver,点击Next;

在Class File Location:选择Axis2Service1\bin目录,点击Next;

勾选Skip WSDL,点击Next;

Service Archiver 选择jar位置,没有jar包就直接点击Next;

勾选Generate the service xml automatically 自动生成XML file文件,点击Next;

service name,输入AxisService(名字可以自己定),然后在class name 中填写要发布的类(全路径),点击load,勾选search declared methods only,点击Next;



output File location ,output File Name输入artiver文件的名称AxisService。点击finish,提示Service Archvie generated successfully!表明生成成功。

3. 发布AxisService

AxisService.aar复制到%TOMCAT-HOME%/webapps/axis/WEB-INF/services下。(不打aar包,/Axis2Service1/deploy/下面复制过去也可以)

打开 http://localhost:8080/axis2/services/listServices 可以看到如下页面



AXIS2调用Web Services

一. 客户端stub文件生成

1. 脚本生成方式

在%AXIS2_HOME%/bin/ 目录下执行下面的命令wsdl2java -uri http://localhost:8080/axis2/services/AxisService?wsdl -p ws -s -o stub说明:-p 指定了生成的java类的报名;-o指定了生成的一系列文件保存的目录;在stub/src/ws
自动生成AxisServiceStub.java

2. 插件生成方式

IDE中选择New -> other -> Axis2 Code Generator, 点击Next;

勾选Generate Java source code from a SWDL file,点击Next;

WSDL file location输入:http://localhost:8080/axis2/services/AxisService?wsdl正确的话,点击Next;

指定输入路径,点击Next;

提示 All operation completed successfully!生成成功。在D:\src\wc 自动生成了stub一系列文件,其中ws是包名;

注:错误 An error occurred while completing process-java.lang.reflect.InvocationTargetException解决方法

1. 从AXIS2的LIB库中复制"geronimo-stax-api_1.0_spec-1.0.1.jar"和"backport-util- concurrent-3.1.jar"

文件到Axis2_Codegen_Wizard_1.3.0的lib目录中,同时修改 Axis2_Codegen_Wizard_1.3.0

下的plugin.xml文件,在文件中中添加:

<library name="lib/geronimo-stax-api_1.0_spec-1.0.1.jar">

<export name="*"/>

</library>

<library name="lib/backport-util-concurrent-3.1.jar">

<export name="*"/>

</library>

如没有backport-util-concurrent-3.1.jar,可以从地址:http://backport-jsr166.sourceforge.net/     下载。

2.将 $workspace位置\.metadata\.plugins下目录 Axis2_Codegen_Wizard 删除。

3.在命令行下切换至$ECLIPSE_HOME目录,然后执行:eclipse –clear。

如还决有问题参考如下解决办法:修改名字,再改回来

二. 客户端调用

脚本生成方式为例,插件生成的类似。
1. 新建 java工程 Axis2Client;
     将%AXIS2_HOME%\lib\目录下的 jar包复制到\Axis2Client\lib\ , 并加入工程引用中,将通过脚本生成的单一AxisServiceStub.java文件 加入到src\ws、下;
2. 新建TestWs.java 代码如下

package ws;

import java.rmi.RemoteException;

public class Axis2Client {
public static void main(String [] args) throws RemoteException{
//初始化Stub类
AxisServiceStub stub = new AxisServiceStub();
//传递AxisServiceStub.ShowName对象,相关参数在这边赋值
AxisServiceStub.ShowName command = new AxisServiceStub.ShowName();
command.setName("Hello!");
//取得返回值
String name = stub.showName(command).get_return();
System.out.println(name);
}

}


3. 调用成功后控制台输出:Hello!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息