axis2开发web services实例


1.安装axis2.下载axis2发布包axis2-1.4.1-war.zip,解压后激昂axis2.war复制到tomcat下的webapps目录下。下载地址:http://ws.apache.org/axis2/

2.验证是否发布成功。启动tomcat,打开“http://localhost:8080/axis2”,可以看到axis2的欢迎界面。点击“Services”可以查看当前已经发布的服务。

3.创建要发布的服务类SampleService.java。
 

  1. package axis2;
  2.  import javax.xml.stream.XMLStreamException;
  3.  import org.apache.axiom.om.OMAbstractFactory;
  4.  import org.apache.axiom.om.OMElement;
  5.  import org.apache.axiom.om.OMFactory;
  6.  import org.apache.axiom.om.OMNamespace;
  7.  public class SampleService {
  8.   public OMElement sayHello(OMElement element) throws XMLStreamException {
  9.    element.build();
  10.    element.detach();
  11.    String rootName = element.getLocalName();
  12.    System.out.println("Reading " + rootName + " element");
  13.    OMElement childElement = element.getFirstElement();
  14.    String personToGreet = childElement.getText();
  15.    OMFactory fac = OMAbstractFactory.getOMFactory();
  16.    OMNamespace omNs = fac.createOMNamespace("http://example1.org/example1""example1");
  17.    OMElement method = fac.createOMElement("sayHelloResponse", omNs);
  18.    OMElement value = fac.createOMElement("greeting", omNs);
  19.    value.addChild(fac.createOMText(value, "Hello," + personToGreet));
  20.    method.addChild(value);
  21.    return method;
  22.   }
  23.   private void ping() {
  24.   }
  25.  }

4.创建服务描述文件services.xml。服务描述文件定义了可用的消息接收类。

 

  1.  <service name="UserGuideSampleService">
  2.      <description>
  3.   This is a sample service created in the Axis2 User's Guide
  4.      </description>
  5.      <parameter name="ServiceClass">
  6.   axis2.SampleService
  7.      </parameter>
  8.      <operation name="sayHello">
  9.   <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
  10.      </operation>
  11.      <operation name="ping">
  12.   <messageReceiver class="org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver"/>
  13.      </operation>
  14.  </service>

5.生成aar文件。在根目录下新建一个名为META-INF的文件夹,此文件夹与class下的axis2目录位于同一位置。将services.xml文件复制到META-INFO目录下。此时的文件路径如下:
      根目录
        |- META-INF
        |      |-services.xml
        |
        |- axis2
        |    |-SampleService.class

 

从命令行进入根目录,执行命令: 

  1. jar cvf SampleService.aar ./*

生成SampleService.aar文件。
这一步也可以直接从eclipse中导出jar文件,然后将扩展名修改为.aar。

6.发布服务。将 SampleService.aar复制到%TOMCAT_HOME%/webapps/axis2/WEB-INF/services目录下。打开该目录下的services.list文件,在末尾增加一行,“SampleService.aar”。

7.查看服务。打开“http://localhost:8080/axis2”,点击“Services”可以看到刚才发布的服务了。

8.创建客户端调用程序。执行前确保已经将axis2.war/lib目录下的jar包引入工程。

 

  1. package axis2;
  2.  import org.apache.axiom.om.OMAbstractFactory;
  3.  import org.apache.axiom.om.OMElement;
  4.  import org.apache.axiom.om.OMFactory;
  5.  import org.apache.axiom.om.OMNamespace;
  6.  import org.apache.axis2.Constants;
  7.  import org.apache.axis2.addressing.EndpointReference;
  8.  import org.apache.axis2.client.Options;
  9.  import org.apache.axis2.client.ServiceClient;
  10.  public class SampleClient {
  11.   private static EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/UserGuideSampleService");
  12.   public static OMElement greetUserPayload(String personToGreet) {
  13.    OMFactory fac = OMAbstractFactory.getOMFactory();
  14.    OMNamespace omNs = fac.createOMNamespace("http://example1.org/example1""example1");
  15.    OMElement method = fac.createOMElement("sayHello", omNs);
  16.    OMElement value = fac.createOMElement("personToGreet", omNs);
  17.    value.addChild(fac.createOMText(value, personToGreet));
  18.    method.addChild(value);
  19.    return method;
  20.   }
  21.   public static void main(String[] args) {
  22.    try {
  23.     OMElement payload = SampleClient.greetUserPayload("John");
  24.     Options options = new Options();
  25.     options.setTo(targetEPR);
  26.     options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
  27.     ServiceClient sender = new ServiceClient();
  28.     sender.setOptions(options);
  29.     OMElement result = sender.sendReceive(payload);
  30.     String response = result.getFirstElement().getText();
  31.     System.out.println(response);
  32.    } catch (Exception e) { //(XMLStreamException e) {
  33.     System.out.println(e.toString());
  34.    }
  35.   }
  36.  }

执行程序,可以看到输出“Hello,John”,调用成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值