微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

axis2 webservice 小例子

参考网上写的。

 

1 在Apache 的官网下载axis2 的包,这时候下载axis2.war.zip 的包,

   下载后解压,将axis2.war包放到tomcat 的webapps下面,启动tomcat

 

2 在地址栏中输入http://localhost:8899/axis2

   出现熟悉的界面,说明Ok了,一般都是好的。

 

 

3 好了我们自学着写个例子,这时候可以参考axis2中自带一个version的例子。

 

    简单的还是从Hello例子写起。

    /**

 * 

 */

package axis2;

 

public class Hello {

 

public String hello(){

return "Hello test";

 }

}

 

建立meta-inf目录,编写services.xml文件

<service name="Hello">

<Description>

HEllO example description

    </Description>

<parameter name="ServiceClass" locked="false">axis2.Hello</parameter>

<operation name="hello">

<messageReceiver class="org.apache.axis2.rpc.receivers.RPcmessageReceiver" />

</operation>

</service>

 

将整个工程打成jar包,放到axis2下的WEB-INF下的services下,并改后缀为aar,重启tomcat。

http://localhost:8080/axis2/services/listServices,这是就可以看到服务了。(最好在IE中,像chrom就看不到wsdl)

 

 

 

========编写客户端测试程序===================

public class Exec {

 

/**

* @param args

* @throws ServiceException

* @throws MalformedURLException

* @throws remoteexception

*/

public static void main(String[] args) throws ServiceException,

MalformedURLException,remoteexception {

// 1 method1

RPCServiceClient rpcclient = new RPCServiceClient();

Options opt = new Options();

opt.setTo(new EndpointReference(

"http://localhost:8899/axis2/services/Hello")); // 服务地址

opt.setAction("urn:hello"); // 方法

rpcclient.setoptions(opt);

OMElement element = rpcclient.invokeBlocking(new QName("http://axis2",  //查看wsdl时可以找到的

"hello"),new Object[] { null }); // null表示没有参数传递

 

Iterator values = element.getChildrenWithName(new QName("http://axis2",

"return")); // return表示有返回值

System.out.println(values);

while (values.hasNext()) { // 遍历出获取的数据

OMElement omElement = (OMElement) values.next();

System.out.println(omElement.getText());

}

 

// 2 method2

String method = "hello";

Service service = new Service();

Call call = (Call) service.createCall();

call.setTargetEndpointAddress(new java.net.URL(

"http://localhost:8899/axis2/services/Hello"));

call.setoperationName(new QName("http://com/",method));

call.setUseSOAPAction(true);

call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);

call.setSOAPActionURI("http://com/GetServerList");

String k = (String) call.invoke(new Object[] {}); //

// 因为返回值是String类型,所以这里调用的返回值也是String类型

System.out.println(">>> " + k); // 返回值输出

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐