Web Service AXIS2入门示例

香帅 2011-10-28

环境:jdk1.5、tomcat5.5;

首先到官方网站下载AXIS2相关jar包;

执行类SimpleService:

publicclassSimpleService

{

publicStringhelloWorld(Stringname)

{

return"你好"+name;

}

publicStringgetInfoTest(){

return"出来的感觉真的可以这样》》》";

}

}

WEB-INF/services/axis/META-INF/services.xml配置:

<?xmlversion="1.0"encoding="UTF-8"?>

<servicename="Hello"scope="application">

<description>axis2入门</description>

<messageReceivers>

<messageReceivermep="http://www.w3.org/2004/08/wsdl/in-only"

class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>

<messageReceivermep="http://www.w3.org/2004/08/wsdl/in-out"

class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>

</messageReceivers>

<!--配置业务实现类-->

<parametername="ServiceClass">com.yss.ws.SimpleService</parameter>

</service>

web.xml文件将Axis2WEB-INFO下的copy过来就OK。

测试类:

importjavax.xml.namespace.QName;

importorg.apache.axis2.addressing.EndpointReference;

importorg.apache.axis2.client.Options;

importorg.apache.axis2.rpc.client.RPCServiceClient;

publicclassRPCClientTest

{

publicstaticvoidmain(String[]args)throwsException

{

//使用RPC方式调用WebService

RPCServiceClientserviceClient=newRPCServiceClient();

Optionsoptions=serviceClient.getOptions();

//指定调用WebService的URL

EndpointReferencetargetEPR=newEndpointReference("http://192.168.72.41:8080/axis_yss/services/Hello?wsdl");

//设置wsdl访问路劲

options.setTo(targetEPR);

//指定getGreeting方法的参数值

Object[]opAddEntryArgs=newObject[]{"helloworld"};

//指定getGreeting方法返回值的数据类型的Class对象

Class[]classes=newClass[]{String.class};

//指定要调用的getGreeting方法及WSDL文件的命名空间

QNameopAddEntry=newQName("http://ws.yss.com","getInfoTest");

//调用getGreeting方法并输出该方法的返回值

System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,classes)[0]);

//设置传入参数类型以及数量

classes=newClass[]{String.class};

//设置wsdl中的targetNamespace和业务实现方法名称

opAddEntry=newQName("http://ws.yss.com","helloWorld");

//显示返回数据

System.out.println(serviceClient.invokeBlocking(opAddEntry,newObject[]{"超人"},classes)[0]);

}

}

运行看是否执行成功。。。

相关推荐