日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

基于Tomcat5.0和Axis2开发Web Service代码详解

發(fā)布時間:2025/7/14 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于Tomcat5.0和Axis2开发Web Service代码详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
本文將詳細(xì)介紹HelloWorld中使用的serverclient端代碼。閱讀之前,你應(yīng)該首先了解SOAP1.1協(xié)議。<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

author: ZJ <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />07-3-13 Blog: [url]http://zhangjunhd.blog.51cto.com/[/url]

?

1HelloWorld做了些什么?

HelloWorld功能非常簡單,在客戶端輸入你的姓名,本例中為ZJ。參數(shù)傳遞到服務(wù)器端后,經(jīng)過處理將返回name+"HelloWorld!",本例中為ZJ HelloWorld!

?

2.服務(wù)器端文件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 {

??? //讀取clientgetSayHelloOMElement()方法傳遞的參數(shù)in

?????? public OMElement sayHello(OMElement in){

??????? //in轉(zhuǎn)換為String

????????????? String name=in.getText(); ????????????? String info=name+"HelloWorld!"; ??????? //創(chuàng)建response SOAP包。 ????????????? OMFactory fac=OMAbstractFactory.getOMFactory(); ??????? // OMNamespace指定此SOAP文檔名稱空間。 ????????????? OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw"); ??????? //創(chuàng)建元素sayHello,并指定其在omNs指代的名稱空間中。 ????????????? OMElement resp=fac.createOMElement("sayHelloResponse",omNs); ??????? //指定元素的文本內(nèi)容。 ????????????? resp.setText(info); ????????????? return resp; ?????? } }

?

3services.xml部署文件

services.xml

<?xml version="1.0" encoding="UTF-8"?> //下面定義服務(wù)名 <service name="HelloWorld"> <description> ? This is a sample Web Service. </description> // ServiceClass指定Java Class的位置,即實現(xiàn)服務(wù)的類。 <parameter name="ServiceClass" locked="false">sample.HelloWorld</parameter> // operation Java Class中方法名對應(yīng)。 <operation name="sayHello"> // messageReceiver看下文注解。 ? <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/> </operation> </service>

?

注解:消息交換模式。

目前Axis2支持三種模式:In-Only、Robust-InIn-Out。In-Only消息交換模式只有SOAP請求,而不需要應(yīng)答;Robust-In消息交換模式發(fā)送SOAP請求,只有在出錯的情況下才返回應(yīng)答;In-Out消息交換模式總是存在SOAP請求和應(yīng)答。本例使用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(){ ??????? //創(chuàng)建request SOAP包。 ????????????? OMFactory fac=OMAbstractFactory.getOMFactory(); ??????? // OMNamespace指定此SOAP文檔名稱空間。 ????????????? OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");

??????? //創(chuàng)建元素sayHello,并指定其在omNs指代的名稱空間中。

????????????? OMElement method=fac.createOMElement("sayHello",omNs); ??????? //指定元素的文本內(nèi)容。 ????????????? 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(); ??????????? //發(fā)出request SOAP //同時將得到的遠(yuǎn)端由sayHello方法返回的信息保存到result //通過services.xml能準(zhǔn)確找到sayHello方法所在的文件。 ???????????????????? OMElement result=sender.sendReceive(sayHello); ????????????? } ????????????? catch(Exception axisFault){ ???????????????????? axisFault.printStackTrace(); ????????????? } ?????? } }

?

5Axis2簡介

Apache Axis2 Axis的后續(xù)版本,是新一代的SOAP引擎。Axis2的主要特點有: 1)采用名為 AXIOMAXIs Object Model)的新核心 XML 處理模型,利用新的XML解析器提供的靈活性按需構(gòu)造對象模型。

?

2)支持不同的消息交換模式。目前Axis2支持三種模式:In-Only、Robust-InIn-Out。In-Only消息交換模式只有SOAP請求,而不需要應(yīng)答;Robust-In消息交換模式發(fā)送SOAP請求,只有在出錯的情況下才返回應(yīng)答;In-Out消息交換模式總是存在SOAP請求和應(yīng)答。

?

3)提供阻塞和非阻塞客戶端 API。

?

4)支持內(nèi)置的 Web服務(wù)尋址 (WS-Addressing) 。

?

5)靈活的數(shù)據(jù)綁定,可以選擇直接使用 AXIOM,使用與原來的 Axis 相似的簡單數(shù)據(jù)綁定方法,或使用 XMLBeans、JiBX JAXB 2.0 等專用數(shù)據(jù)綁定框架。

?

6)新的部署模型,支持熱部署。

?

7)支持HTTPSMTPJMSTCP傳輸協(xié)議。

?

8)支持REST (Representational State Transfer)。

?

6Axis2 支持的規(guī)范包括:

-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 有關(guān)Axis2更加詳細(xì)的介紹,可以訪問Axis2網(wǎng)站[url]http://ws.apache.org/axis2/[/url]。

?

相關(guān)介紹?

基于Tomcat5.0和Axis2開發(fā)Web Service應(yīng)用實例

轉(zhuǎn)載于:https://blog.51cto.com/zhangjunhd/23692

總結(jié)

以上是生活随笔為你收集整理的基于Tomcat5.0和Axis2开发Web Service代码详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。