准备工作
1、下载:axis2-1.5.4-bin.zip,axis2-1.5.4-war.zip 下载地址:http://axis.apache.org/axis2/java/core/
2、环境变量设置
AXIS2_HOME E:\research\axis2-1.5.4-bin\axis2-1.5.4
JAVA_HOME C:\Program Files\Java\jdk1.6.0_21
3、axis2-1.5.4-war.zip解压,将压缩包内的axis2.war部署到%TOMCAT-HOME%/webapps下,启动tomcat,访问http://localhost:8085/axis2/看是否正常。
点击Service会进入Service列表页面,当前只有一个Version服务。http://localhost:8085/axis2/services/Version?wsdl
4、下载 axis2-eclipse-codegen-plugin-1.5.4.zip,axis2-eclipse-service-plugin-1.5.4.zip 解压后将plugins 复制到 %ECLIPSE_HOME%\plugins。
http://www.apache.org/dyn/mirrors/mirrors.cgi/axis/axis2/java/core/1.5.4/axis2-eclipse-codegen-plugin-1.5.4.zip
http://www.apache.org/dyn/mirrors/mirrors.cgi/axis/axis2/java/core/1.5.4/axis2-eclipse-service-plugin-1.5.4.zip
安装完插件后,IDE中选择new->other会看到下面界面
如果安装Axis2插件之后,在eclipse中没有出现界面,就换一个eclipse版本
在版本比较新的eclipse中,安装Axis插件,是把jar复制到%ECLIPSE_HOME%\dropins目录下,而不是plugins目录
AXIS2发布Web Services
一、工程文件
1、新建 Axis2Service1 java工程。
2、新建 \Axis2Service1\src\ws\TestWs.java
package ws;
public class TestWs {
public String showName(String name) {return name; }
public String getName() {return "Axis2Service Sample"; }
}
二、arr部署方式
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>
生成aar包 \Axis2Service1\deploy>jar cvf AxisService.aar . (注意带.号)
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,输入:D:\ ; output File Name,输入artiver文件的名称 AxisService。点击finish。
提示 Service Archvie generated successfully! 注册表明,生成成功。
3、发布AxisService
AxisService.aar复制到%TOMCAT-HOME%/webapps/axis2/WEB-INF/services下。(不打aar包,\Axis2Service1\deploy下面复制过去也是可以)
打开http://localhost:8085/axis2/services/listServices 看到
三、独立部署
1、新建java web project工程。
2、文件复制
%TOMCAT-HOME%\webapps\axis2\WEB-INF\lib 复制到 \Axis2Service2\WebRoot\WEB-INF\lib 下,并加入工程引用。
%TOMCAT-HOME%\webapps\axis2\WEB-INF\conf 复制到 \Axis2Service2\WebRoot\WEB-INF\conf
%TOMCAT-HOME%\webapps\axis2\WEB-INF\modules 复制到 \Axis2Service2\WebRoot\WEB-INF\modules
3、web.xml 代码如下
<?xml version="1.0" encoding="UTF-8"?> <web-app id="wmf" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>AxisServlet</servlet-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>/services/*</url-pattern> </servlet-mapping> </web-app>
2、新建 \Axis2Service2\src\ws\TestWs.java
package ws;
public class TestWs {
public String showName(String name) {return name; }
public String getName() {return "Axis2Service Sample"; }
}
3、新建\Axis2Service2\WebRoot\WEB-INF\services目录。
4、新建一个AxisService服务
AxisService\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>
启动tomcat后,访问http://localhost:8085/Axis2Service2/services/AxisService?wsdl看是否正常。
上面的文章转载:http://www.lifeba.org/arch/java_axis2_webservice.html
Axis2客户端调用:
以上面的例子为例,调用代码如下:
package ws;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
/**
* @Description: TODO
* @author luickystar2008
* @date 2013-10-14 下午8:01:17
*/
public class TestWs {
/**
*
*/
public static void main(String[] args) {
try {
RPCServiceClient rpcServiceClient = new RPCServiceClient();
Options options = rpcServiceClient.getOptions();
EndpointReference targetEPR = new EndpointReference("http://localhost:8080/Axis2Service2/services/AxisService");
options.setTo(targetEPR);
// 1.有参数,有返回值的调用
String callback = (String) rpcServiceClient.invokeBlocking(new QName("http://ws","showName"), new Object[]{"Hello!"},new Class[]{String.class})[0];
System.out.println(callback);
// 2.无参数,有返回值的方法调用
Object[] obj = rpcServiceClient.invokeBlocking(new QName("http://ws","getName"), new Object[]{},new Class[]{String.class});
System.out.println(obj[0]);
// 3.无参数,无返回值的调用
rpcServiceClient.invokeRobust(new QName("http://ws","hello"), new Object[]{});
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
下面的文章转载:http://blog.163.com/germans@126/blog/static/2697237420138116318275/
在调用一个无参有返回值的方法时抛出如下异常:
1、
当方法体没有参数返回的时候,我们要采用RPCServiceClient.invokeRobust(QName, new Object[]{..});
当有返回参数的时候采用:RPCServiceClient.invokeBlocking(QName, new Object[]{..},new Class[]{..})
2、services.xml配置中,
<operation name="getHello">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
<!-- 有返回值 -->
</operation>
<operation name="hello">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
<!-- 无返回值 -->
</operation>
public String getHello(){return "hello world";}
public String getHello(String name ){return name+" Hello";}
public void hello(String a){ this.a = a;}