加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

基于Axis2开发WebService代码详解

发布时间:2020-12-17 00:22:25 所属栏目:安全 来源:网络整理
导读:基于Axis2开发WebService代码详解 关键字: webservice 转载: http://tenn.javaeye.com/blog/100755 1.HelloWorld做了些什么? HelloWorld功能非常简单,在客户端输入你的姓名,本例中为ZJ。参数传递到服务器端后,经过处理将返回name+"HelloWorld!",本例中

基于Axis2开发WebService代码详解

关键字: webservice
转载: http://tenn.javaeye.com/blog/100755
1.HelloWorld做了些什么?
HelloWorld功能非常简单,在客户端输入你的姓名,本例中为ZJ。参数传递到服务器端后,经过处理将返回name+"HelloWorld!",本例中为ZJ HelloWorld!

2.服务器端文件HelloWorld.java
HelloWorld.java
Java代码

  1. package sample;??
  2. ???
  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. ???
  8. public class HelloWorld {??
  9. ????//读取client端getSayHelloOMElement()方法传递的参数in。??
  10. ???????public OMElement sayHello(OMElement in){??
  11. ????????//将in转换为String。??
  12. ?????????????? String name=in.getText();??
  13. ?????????????? String info=name+"HelloWorld!";??
  14. ????????//创建response SOAP包。??
  15. ?????????????? OMFactory fac=OMAbstractFactory.getOMFactory();??
  16. ????????// OMNamespace指定此SOAP文档名称空间。??
  17. ?????????????? OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");??
  18. ????????//创建元素sayHello,并指定其在omNs指代的名称空间中。??
  19. ?????????????? OMElement resp=fac.createOMElement("sayHelloResponse",omNs);??
  20. ????????//指定元素的文本内容。??
  21. ?????????????? resp.setText(info);??
  22. ??????????????return resp;??
  23. ??????? }??
  24. }???

3.services.xml部署文件
services.xml
Java代码

  1. <?xml version="1.0" encoding="UTF-8"?>??
  2. //下面定义服务名??
  3. <service name="HelloWorld">??
  4. <description>??
  5. ?? This is a sample Web Service.??
  6. </description>??
  7. // ServiceClass指定Java Class的位置,即实现服务的类。??
  8. <parameter name="ServiceClass" locked="false">sample.HelloWorld</parameter>??
  9. // operation 与Java Class中方法名对应。??
  10. <operation name="sayHello">??
  11. // messageReceiver看下文注解。??
  12. ?? <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>??
  13. </operation>??
  14. </service>???


注解:消息交换模式。
目前Axis2支持三种模式:In-Only、Robust-In和In-Out。In-Only消息交换模式只有SOAP请求,而不需要应答; Robust-In消息交换模式发送SOAP请求,只有在出错的情况下才返回应答;In-Out消息交换模式总是存在SOAP请求和应答。本例使用In- Out模式。

4.客户端文件TestClient.java
Java代码

  1. TestClient.java??
  2. package example.client;??
  3. ???
  4. import org.apache.axiom.om.OMAbstractFactory;??
  5. import org.apache.axiom.om.OMElement;??
  6. import org.apache.axiom.om.OMFactory;??
  7. import org.apache.axiom.om.OMNamespace;??
  8. import org.apache.axis2.addressing.EndpointReference;??
  9. import org.apache.axis2.client.Options;??
  10. import org.apache.axis2.client.ServiceClient;??
  11. ???
  12. public class TestClient {??
  13. ????// targetEPR指定打包的Service(.aar文件)在容器中的物理位置。??
  14. ???????private static EndpointReference targetEPR=new EndpointReference??
  15. ????????? ("http://localhost:8080/axis2/services/HelloWorld");??
  16. ???????public static OMElement getSayHelloOMElement(){??
  17. ????????//创建request SOAP包。??
  18. ?????????????? OMFactory fac=OMAbstractFactory.getOMFactory();??
  19. ????????// OMNamespace指定此SOAP文档名称空间。??
  20. ?????????????? OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");??
  21. ????????//创建元素sayHello,并指定其在omNs指代的名称空间中。??
  22. ?????????????? OMElement method=fac.createOMElement("sayHello",omNs);??
  23. ????????//指定元素的文本内容。??
  24. ?????????????? method.setText("ZJ");??
  25. ??????????????return method;??
  26. ??????? }??
  27. ???????public static void main(String[] args){??
  28. ??????????????try{??
  29. ????????????????????? Options options=new Options();??
  30. ????????????????????? options.setTo(targetEPR);??
  31. ????????????????????? ServiceClient sender=new ServiceClient();??
  32. ????????????????????? sender.setOptions(options);??
  33. ????????????????????? OMElement sayHello=TestClient.getSayHelloOMElement();??
  34. ????????????//发出request SOAP,??
  35. //同时将得到的远端由sayHello方法返回的信息保存到result。??
  36. //通过services.xml能准确找到sayHello方法所在的文件。??
  37. ????????????????????? OMElement result=sender.sendReceive(sayHello);??
  38. ?????????????? }??
  39. ??????????????catch(Exception axisFault){??
  40. ????????????????????? axisFault.printStackTrace();??
  41. ?????????????? }??
  42. ??????? }??
  43. }???
5.Axis2简介 Apache Axis2 是Axis的后续版本,是新一代的SOAP引擎。Axis2的主要特点有: 1)采用名为 AXIOM(AXIs Object Model)的新核心 XML 处理模型,利用新的XML解析器提供的灵活性按需构造对象模型。 2)支持不同的消息交换模式。目前Axis2支持三种模式:In-Only、Robust-In和In-Out。In-Only消息交换模式只有 SOAP请求,而不需要应答;Robust-In消息交换模式发送SOAP请求,只有在出错的情况下才返回应答;In-Out消息交换模式总是存在 SOAP请求和应答。 3)提供阻塞和非阻塞客户端 API。 4)支持内置的 Web服务寻址 (WS-Addressing) 。 5)灵活的数据绑定,可以选择直接使用 AXIOM,使用与原来的 Axis 相似的简单数据绑定方法,或使用 XMLBeans、JiBX 或 JAXB 2.0 等专用数据绑定框架。 6)新的部署模型,支持热部署。 7)支持HTTP,SMTP,JMS,TCP传输协议。 8)支持REST (Representational State Transfer)。 6.Axis2 支持的规范包括: -SOAP 1.1 and 1.2 -Message Transmission Optimization Mechanism (MTOM),XML Optimized Packaging (XOP) and SOAP with Attachments -WSDL 1.1,including both SOAP and HTTP bindings -WS-Addressing (submission and final) -WS-Policy -SAAJ 1.1 有关Axis2更加详细的介绍,可以访问Axis2网站http://ws.apache.org/axis2/。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读