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

//services.xml内容如下
//发布服务的名称
<service name="HelloWorld">
 //针对此服务的描述
    <description>
        This is a sample Web Service with two operations.
  </description>
  //指定实现服务的类路径
  <parameter name="ServiceClass">com.sk.service.HelloWorld</parameter>
  //指定实现服务的方法
  <operation name="sayHello">
  //指定此方法的消息交换模式
        <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
    </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)查看刚才发布的服务
具体步骤:
 1.在工程上右键,选Export-->Java-->Jar file [next]
 2.将文件命名为test.aar [finish]
 3.用winrar打开test.aar文件
 4.把services.xml文件copy到META-INF目录下


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

}
结束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gamebox1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值