1、新建web项目,结构如下
Axis2
| src
| WebRoot
| | WEB-INF
| | |lib
| | |web.xml
| | index.jsp
将axis2-1.6.0-war.zip解压至tomcat的webapps目录,启动tomcat后会自动解压axis2.war
项目,将axis2工程下axis2-web拷贝至WebRoot下,同时将axis2中lib目录下的jar包全部
拷贝至项目中lib下,保留如下文件即可,目录结构如下
Axis2
| src
| WebRoot
| | axis2-web
| | | css
| | | images
| | | include
| | | index.jsp
| | | listServices.jsp
| | WEB-INF
| | | lib
| | | web.xml
| | index.jsp
2、在web.xml中添加如下代码
<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>
3、新建HelloService.java类,代码如下:
package com.siven.axis2.service;
public class HelloService
{
public String sayHello()
{
return "hello";
}
public String sayHelloToPerson(String name)
{
if (name == null)
{
name = "nobody";
}
return "hello," + name;
}
}
4、在项目WEB-INF目录下新增如下目录及文件,结构如下
Axis2
| src
| WebRoot
| | axis2-web
| | | css
| | | images
| | | include
| | | index.jsp
| | | listServices.jsp
| | WEB-INF
| | | lib
| | | services
| | | |MyService
| | | | |META-INF
| | | | | | services.xml
| | | web.xml
| | index.jsp
services.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
<service name="helloService">
<description>Web Service例子</description>
<parameter name="ServiceClass">com.siven.axis2.service.HelloService</parameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
</messageReceivers>
</service>
</serviceGroup>
5、将Axis2项目发布至tomcat中,启动tomcat,在浏览器中输入:
http://localhost:8888/Axis2/services/listServices
如果看到如下界面,则证明Service发布成功
Available services
helloService
Service Description : helloService
Service EPR : http://localhost:8888/Axis2/services/helloService
Service Status : Active
Available Operations
sayHello
sayHelloToPerson
6、以下是客户端调用代码
public static void invokeSayHello() throws AxisFault
{
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference("http://localhost:8888/Axis2/services/helloService");
options.setTo(targetEPR);
// 指定sayHelloToPerson方法返回值的数据类型的Class对象
@SuppressWarnings("rawtypes")
Class[] classes = new Class[]{String.class};
// 指定要调用的sayHelloToPerson方法及WSDL文件的命名空间
QName opAddEntry = new QName("http://service.axis2.siven.com", "sayHello");
// 调用sayHelloToPerson方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry, new Object[]{}, classes)[0]);
}
public static void invokeSayHelloToPerson() throws AxisFault
{
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference("http://localhost:8888/Axis2/services/helloService");
options.setTo(targetEPR);
// 指定sayHelloToPerson方法的参数值
Object[] opAddEntryArgs = new Object[]{"siven"};
// 指定sayHelloToPerson方法返回值的数据类型的Class对象
@SuppressWarnings("rawtypes")
Class[] classes = new Class[]{String.class};
// 指定要调用的sayHelloToPerson方法及WSDL文件的命名空间
QName opAddEntry = new QName("http://service.axis2.siven.com", "sayHelloToPerson");
// 调用sayHelloToPerson方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
}
7、以下是Stub调用方式:
前提:安装好Axis2,并配置环境变量
AXIS2_HOME:Axis2安装目录
在cmd下运行如下命令:
%AXIS2_HOME%\bin\wsdl2java -uri http://localhost:8888/Axis2/services/helloService?wsdl -p com.siven.axis2.client -s -o stub
其中-url参数指定了wsdl文件的路径,可以是本地路径,也可以是网络路径。
-p参数指定了生成的Java类的包名,-o参数指定了生成的一系列文件保存的根目录。
在执行完上面的命令后,就会发现在当前目录下多了个stub目录,
在stub/src/client目录可以找到一个HelloServiceStub.java文件,
调用代码:
HelloServiceStub stub = new HelloServiceStub();
HelloServiceStub.SayHelloToPerson gg = new HelloServiceStub.SayHelloToPerson();
gg.setName("siven");
System.out.println(stub.sayHello(new HelloServiceStub.SayHello()).get_return());
System.out.println(stub.sayHelloToPerson(gg).get_return());
axis2入门
猜你喜欢
转载自hwei-344370758.iteye.com/blog/1884955
今日推荐
周排行