基于Tomcat5.0和Axis2开发Web Service代码详解
2009-07-31 22:49
513 查看
1
.
HelloWorld
做了些什么?
HelloWorld
功能非常简单,在客户端输入你的姓名,本例中为
ZJ
。参数传递到服务器端后,经过处理将返回
name+"HelloWorld!"
,本例中为
ZJ HelloWorld!
2
.服务器端文件
HelloWorld.java
HelloWorld.java
3
.
services.xml
部署文件
services.xml
注解:
消息交换模式。
目前
Axis2
支持三种模式:
In-Only
、
Robust-In
和
In-Out
。
In-Only
消息交换模式只有
SOAP
请求,而不需要应答;
Robust-In
消息交换模式发送
SOAP
请求,只有在出错的情况下才返回应答;
In-Out
消息交换模式总是存在
SOAP
请求和应答。本例使用
In-Out
模式。
4
.客户端文件
TestClient.java
TestClient.java
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/
。
.
HelloWorld
做了些什么?
HelloWorld
功能非常简单,在客户端输入你的姓名,本例中为
ZJ
。参数传递到服务器端后,经过处理将返回
name+"HelloWorld!"
,本例中为
ZJ HelloWorld!
2
.服务器端文件
HelloWorld.java
HelloWorld.java
package sample; 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 端 getSayHelloOMElement() 方法传递的参数 in 。 public OMElement sayHello(OMElement in){ // 将 in 转换为 String 。 String name=in.getText(); String info=name+"HelloWorld!"; // 创建 response SOAP 包。 OMFactory fac=OMAbstractFactory.getOMFactory(); // OMNamespace 指定此 SOAP 文档名称空间。 OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw"); // 创建元素 sayHello ,并指定其在 omNs 指代的名称空间中。 OMElement resp=fac.createOMElement("sayHelloResponse",omNs); // 指定元素的文本内容。 resp.setText(info); return resp; } } |
.
services.xml
部署文件
services.xml
<?xml version="1.0" encoding="UTF-8"?> // 下面定义服务名 <service name="HelloWorld"> <description> This is a sample Web Service. </description> // ServiceClass 指定 Java Class 的位置,即实现服务的类。 <parameter name="ServiceClass" locked="false">sample.HelloWorld</parameter> // operation 与 Java Class 中方法名对应。 <operation name="sayHello"> // messageReceiver 看下文注解。 <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
模式。
4
.客户端文件
TestClient.java
TestClient.java
package example.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.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; public class TestClient { // targetEPR 指定打包的 Service ( .aar 文件 ) 在容器中的物理位置。 private static EndpointReference targetEPR=new EndpointReference ("http://localhost:8080/axis2/services/HelloWorld"); public static OMElement getSayHelloOMElement(){ // 创建 request SOAP 包。 OMFactory fac=OMAbstractFactory.getOMFactory(); // OMNamespace 指定此 SOAP 文档名称空间。 OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw"); // 创建元素 sayHello ,并指定其在 omNs 指代的名称空间中。 OMElement method=fac.createOMElement("sayHello",omNs); // 指定元素的文本内容。 method.setText("ZJ"); return method; } public static void main(String[] args){ try{ Options options=new Options(); options.setTo(targetEPR); ServiceClient sender=new ServiceClient(); sender.setOptions(options); OMElement sayHello=TestClient.getSayHelloOMElement(); // 发出 request SOAP , // 同时将得到的远端由 sayHello 方法返回的信息保存到 result 。 // 通过 services.xml 能准确找到 sayHello 方法所在的文件。 OMElement result=sender.sendReceive(sayHello); } catch(Exception axisFault){ axisFault.printStackTrace(); } } } |
.
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/
。
相关文章推荐
- 基于Tomcat5.0和Axis2开发Web Service代码详解
- 基于Tomcat5.0和Axis2开发Web Service代码详解
- 基于Tomcat5.0和Axis2开发Web Service代码详解
- 基于Tomcat5.0和Axis2开发Web Service应用实例
- 基于Tomcat5.0和Axis2开发Web Service应用实例
- 基于Tomcat5.0和Axis2开发Web Service应用实例
- 基于Tomcat5.0和Axis2开发Web Service应用实例
- 基于Tomcat5.0和Axis2开发Web Service应用实例
- 基于Tomcat5.0和Axis2开发Web Service应用实例
- 基于Tomcat5.0和Axis2开发Web Service应用实例
- 基于Tomcat5.0和Axis2开发Web Service应用实例
- 基于Axis2开发WebService代码详解
- 基于Axis2开发WebService代码详解
- 基于Tomcat和Axis2开发Web Service应用实例
- [Axis2与Eclipse整合开发Web Service系列之一] 生成Web Service Client(将WSDl 转化成 Java代码)
- java基于Axis2调用C#开发的web service获取xml结果
- [Axis2与Eclipse整合开发Web Service系列之一] 生成Web Service Client(将WSDl 转化成 Java代码)
- 基于 tomcat 的 JSP 开发环境手动配置详解
- jersey 基于tomcat web service 开发 restful service
- 微信支付 - 基于H5的微信支付开发步骤及代码详解