手把手教你写一个最最最简单的axis2 webservice


1.下载axis2
################################
环境:eclipse3.2

AXIS2(SOAP引擎)
AXIS2下载页面:http://ws.apache.org/axis2/download.cgi

axis2-1.3-bin.zip下载链接:
http://apache.mirror.phpchina.com/ws/axis2/1_3/axis2-1.3-bin.zip
解压缩axis2-1.3-bin.zip,lib目录下的jar包是WebService业务开发需要的库文件。

axis2-1.3-war.zip下载链接:
http://apache.mirror.phpchina.com/ws/axis2/1_3/axis2-1.3-war.zip
################################
2.配置好java环境,axis2环境(具体博客中有)
3.部署axis2的服务

解压缩axis2-1.3-war.zip后,将axis2.war复制到<Tomcat目录>\webapps 下
首先,创建一个Java工程,写好包servcie,client
1.写service(服务端)方法
package com.sk.service;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;

public class HelloWorld {
<wbr></wbr>//读取Client端sayHello节点
<wbr></wbr>public OMElement sayHello(OMElement in){
<wbr></wbr><wbr></wbr>//获取节点的内容
<wbr></wbr><wbr></wbr>String name=in.getText();
<wbr></wbr><wbr></wbr>//准备响应信息
<wbr></wbr><wbr></wbr>String info=name+"HelloWorld axis2";
<wbr></wbr><wbr></wbr>//创建response SOAP信息包
<wbr></wbr><wbr></wbr>OMFactory fac=OMAbstractFactory.getOMFactory();
<wbr></wbr><wbr></wbr>//创建response SOAP信息命名空间
<wbr></wbr><wbr></wbr>OMNamespace omNs=fac.createOMNamespace("http://ml.com", "helloWorld");
<wbr></wbr><wbr></wbr>//创建response SOAP信息节点
<wbr></wbr><wbr></wbr>OMElement resp=fac.createOMElement("sayHelloResponse",omNs);
<wbr></wbr><wbr></wbr>//节点添加上准备恢复的信息
<wbr></wbr><wbr></wbr>resp.setText(info);
<wbr></wbr><wbr></wbr>return resp;
<wbr></wbr>}
}
2.发布Service
Axis2发布服务需要services.xml(部署文件)

//services.xml内容如下
//发布服务的名称
<service name="HelloWorld">
<wbr></wbr>//针对此服务的描述
<wbr></wbr><wbr></wbr><wbr></wbr> <description>
<wbr></wbr><wbr></wbr><wbr></wbr><wbr></wbr><wbr></wbr><wbr></wbr><wbr></wbr> This is a sample Web Service with two operations.
  </description>
  //指定实现服务的类路径
  <parameter name="ServiceClass">com.sk.service.HelloWorld</parameter>
  //指定实现服务的方法
  <operation name="sayHello">
  //指定此方法的消息交换模式
<wbr></wbr><wbr></wbr><wbr></wbr><wbr></wbr><wbr></wbr><wbr></wbr><wbr></wbr> <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiv<wbr></wbr>er"/>
<wbr></wbr><wbr></wbr><wbr></wbr> </operation>
</service>
注解:消息交换模式。
目前Axis2支持三种模式:In-Only、Robust-In和In-Out。In-Only消息交换模式只有SOAP请求,而不需要应答;Robust-In消息交换模式发送SOAP请求,只有在出错的情况下才返回应答;In-Out消息交换模式总是存在SOAP请求和应答。本例使用In-Out模式。
3.部署服务
将此java工程导出成jar文件,用winrar打开导出的jar,将写好的services.xml文件拷到META-INF中,保存.将此*.jar改为*.aar,放入axis2\Tomcat 5.5\webapps\axis2\WEB-INF\services下.
在浏览器中查看(http://127.0.0.1:8081/axis2/services/listServices)查看刚才发布的服务
具体步骤:
<wbr></wbr>1.在工程上右键,选Export-->Java-->Jar file [next]
<wbr></wbr>2.将文件命名为test.aar<wbr></wbr>[finish]
<wbr></wbr>3.用winrar打开test.aar文件
<wbr></wbr>4.把services.xml文件copy到META-INF目录下


<wbr></wbr>保存OK
把test.aar文件COPY到tomcat的webapps\axis2\WEB-INF\services目录下.
启动tomcat.
在IE中输入http://127.0.0.1:8080/axis2/
点击Services 将看到刚才发布的webservice
4.写client(客户端发送)
package com.sk.client;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

public class HelloWorldClient {
//设置发送终端
<wbr></wbr>private static EndpointReference targetEPR=new EndpointReference("http://127.0.0.1:8081/axis2/services/HelloWorld");
<wbr></wbr>public static void main(String[] args) {
<wbr></wbr><wbr></wbr>// TODO Auto-generated method stub
<wbr></wbr>//创建request SOAP消息包
<wbr></wbr><wbr></wbr>OMFactory fac=OMAbstractFactory.getOMFactory();
<wbr></wbr>//创建request SOAP 命名空间
<wbr></wbr><wbr></wbr>OMNamespace omNs=fac.createOMNamespace("http://www.ml.com", "MyHelloWorld");
<wbr></wbr><wbr></wbr>//创建调用服务的方法的节点(节点的名称就是要调用服务端的方法名)
<wbr></wbr><wbr></wbr>OMElement element= fac.createOMElement("sayHello",omNs);
<wbr></wbr><wbr></wbr>//设置节点的内容
<wbr></wbr><wbr></wbr>element.setText("Your Name");
<wbr></wbr><wbr></wbr>try {
<wbr></wbr><wbr></wbr><wbr></wbr>//创建选项
<wbr></wbr><wbr></wbr><wbr></wbr>Options options=new Options();
<wbr></wbr><wbr></wbr><wbr></wbr>//设置选项的发送终端
<wbr></wbr><wbr></wbr><wbr></wbr>options.setTo(targetEPR);
<wbr></wbr><wbr></wbr><wbr></wbr>//创建发送客户端
<wbr></wbr><wbr></wbr><wbr></wbr>ServiceClient sender=new ServiceClient();
<wbr></wbr><wbr></wbr><wbr></wbr>//设置客户端的选项
<wbr></wbr><wbr></wbr><wbr></wbr>sender.setOptions(options);
  //发送SOAP消息,并获取service的返回结果
<wbr></wbr><wbr></wbr><wbr></wbr>OMElement resultElement= sender.sendReceive(element);
<wbr></wbr><wbr></wbr><wbr></wbr>System.out.println(resultElement);
<wbr></wbr><wbr></wbr>} catch (AxisFault e) {
<wbr></wbr><wbr></wbr><wbr></wbr>// TODO Auto-generated catch block
<wbr></wbr><wbr></wbr><wbr></wbr>e.printStackTrace();
<wbr></wbr><wbr></wbr>}
<wbr></wbr>}

}
结束

你可能感兴趣的:(apache,tomcat,webservice,IE,SOAP)