AXIS2 WebServices开发实践(一)

来源:互联网 发布:java网络编程第四版 编辑:程序博客网 时间:2024/06/02 15:40

 

1、概述

AXIS2 是Apache SOAP协议的实现。基于TCP/IP协议使用XML实现远程数据通信的一种技术,分为Server与Client。

OS:Windows 7 旗舰版;Java JVM:jdk1.5.0_08;IDE:Eclipse v3.5。

 

2、下载AXIS2

2.1、地址

http://www.apache.org/dist/ws/axis2/1_5_1/

2.2、文件

axis2-1.5.1-bin.zip    // 应用开发只需要下载此文件即可

axis2-1.5.1-docs.zip // 开发中参考文档,推荐下载

axis2-1.5.1-war.zip   // 部署到tomcat,推荐下载

axis2-1.5.1-src.zip    // 源码,推荐下载

axis2-1.5.1.pom      // mavenpom,非必须

如下图所示:

3、设置eclipse axis2 runtime

3.1、解压

解压“axis2-1.5.1-bin.zip”文件到指定目录,例:“d:\develop\java\ws\axis2-1.5.1”;

3.2、eclipse中配置axis2环境

打开“eclipse >>Windows >> Preferences”窗口,选择“Web Services >> Axis2 Preferences”标签页,设置“Axis2Runtime >> Axis2 Runtime Location:”为上面的解压目录。如果显示“Axis2runtime loaded successfully”字样则说明配置成功。如下图所示:

4、创建web service

4.1、创建web工程axis2test

选择“File >> new>> Dynamic Web Project”创建WEB工程;选中工程并点击右键在弹出的快捷菜单中选择“Properties”,在打开的窗口中选择“Project Facets”并在右边的复选框选择“Axis2 Web Services”下的“Axis2 Web Services Core”与“Axis2 Web ServicesExtensions”。如下图所示:

4.2、复制源代码文件到工程中

         复制“org.apache.axis2.webapp”包下的所有文件到src下。

4.3、创建Hello.java

public class Hello {

        public String say(){

           return"昨夜星晨昨夜风";

        }

}

4.4、直接发布Hello为Web service

(1)、选中“Hello.java”点击右键在弹出的快捷菜单中点击“Web Services >> Create Web service”,在弹出的对话框中,选中“Publish the Web service”复选框。

        

(2)、点击“Server:Tomat 6.0”或“Webservice runtime:Apache Axis”连接,在弹出的“Service Deployment Configuration”窗口中,选中“Tomcat v6.0Server at localhost”与“ApacheAxis2”,并点击OK,并点击“Next”。

        

(3)、在之后弹出的窗口中不做更改直接一路“Next”直到最后点击“Finish”即可,其中需要启动tomcat,启动它即可。

        

        

4.5、验证发布结果

4.5.1、查看服务

输入:http://192.168.100.57:8080/axis2test/services/listServices

返回:

        

4.5.2、调用服务

输入:http://192.168.100.57:8080/axis2test/services/Hello/say

返回:

<ns:sayResponsexmlns:ns="http://test.axis2.evensoft.org.cn">

           <ns:return>昨夜星晨昨夜风</ns:return>

  </ns:sayResponse>

 

4.5.3、查看服务wsdl

输入:http://192.168.100.57:8080/axis2test/services/Hello?wsdl

返回:

<?xmlversion="1.0"encoding="UTF8"?>

<wsdl:definitionsxmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"

    xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"

    xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"xmlns:ns1="http://org.apache.axis2/xsd"

    xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl"xmlns:ns="http://test.axis2.evensoft.org.cn"

    xmlns:xs="http://www.w3.org/2001/XMLSchema"xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"

    targetNamespace="http://test.axis2.evensoft.org.cn">

    <wsdl:documentation>Please Type yourservice description here

    </wsdl:documentation>

    <wsdl:types>

       <xs:schemaattributeFormDefault="qualified"

           elementFormDefault="qualified"targetNamespace="http://test.axis2.evensoft.org.cn">

           <xs:elementname="sayResponse">

              <xs:complexType>

                  <xs:sequence>

                     <xs:elementminOccurs="0"name="return"nillable="true"

                         type="xs:string"/>

                  </xs:sequence>

              </xs:complexType>

           </xs:element>

       </xs:schema>

    </wsdl:types>

    <wsdl:messagename="sayRequest"/>

    <wsdl:messagename="sayResponse">

       <wsdl:partname="parameters"element="ns:sayResponse"/>

    </wsdl:message>

    <wsdl:portTypename="HelloPortType">

       <wsdl:operationname="say">

           <wsdl:inputmessage="ns:sayRequest"wsaw:Action="urn:say"/>

           <wsdl:outputmessage="ns:sayResponse"wsaw:Action="urn:sayResponse"/>

       </wsdl:operation>

    </wsdl:portType>

    <wsdl:bindingname="HelloSoap11Binding"type="ns:HelloPortType">

       <soap:bindingtransport="http://schemas.xmlsoap.org/soap/http"

           style="document"/>

       <wsdl:operationname="say">

           <soap:operationsoapAction="urn:say"style="document"/>

           <wsdl:input>

              <soap:bodyuse="literal"/>

           </wsdl:input>

           <wsdl:output>

              <soap:bodyuse="literal"/>

           </wsdl:output>

       </wsdl:operation>

    </wsdl:binding>

    <wsdl:bindingname="HelloSoap12Binding"type="ns:HelloPortType">

       <soap12:bindingtransport="http://schemas.xmlsoap.org/soap/http"

           style="document"/>

       <wsdl:operationname="say">

           <soap12:operationsoapAction="urn:say"style="document"/>

           <wsdl:input>

              <soap12:bodyuse="literal"/>

           </wsdl:input>

           <wsdl:output>

              <soap12:bodyuse="literal"/>

           </wsdl:output>

       </wsdl:operation>

    </wsdl:binding>

    <wsdl:bindingname="HelloHttpBinding"type="ns:HelloPortType">

       <http:bindingverb="POST"/>

       <wsdl:operationname="say">

           <http:operationlocation="Hello/say"/>

           <wsdl:input>

              <mime:contenttype="text/xml"part="say"/>

           </wsdl:input>

           <wsdl:output>

              <mime:contenttype="text/xml"part="say"/>

           </wsdl:output>

       </wsdl:operation>

    </wsdl:binding>

    <wsdl:servicename="Hello">

       <wsdl:portname="HelloHttpSoap11Endpoint"binding="ns:HelloSoap11Binding">

           <soap:address

               location="http://192.168.100.57:8080/axis2test/services/Hello.HelloHttpSoap11Endpoint/"/>

       </wsdl:port>

       <wsdl:portname="HelloHttpSoap12Endpoint"binding="ns:HelloSoap12Binding">

           <soap12:address

               location="http://192.168.100.57:8080/axis2test/services/Hello.HelloHttpSoap12Endpoint/"/>

       </wsdl:port>

       <wsdl:portname="HelloHttpEndpoint"binding="ns:HelloHttpBinding">

           <http:address

               location="http://192.168.100.57:8080/axis2test/services/Hello.HelloHttpEndpoint/"/>

       </wsdl:port>

    </wsdl:service>

</wsdl:definitions>

5、axis2 war中部署Webservice

5.1、打包为aar

         将“WEB-INF/services/Hello”目录下所有文件打包为Hello.zip文件,然后修改扩展名为aar。

 

5.2、复制aar文件至axis war

         将此文件复制到“${TOMCAT_HOME}/webapps/axis2/WEB-INF/services”目录下。其实不打包直接复制过去也可,实际生产境部署中还是打包的好。

         注:copyaxis2.war到“${TOMCAT_HOME}/webapps”下,tomcat启动后会自动生成axis2文件夹。

5.3、验证发布结果

         同4.5(wsdl、listServices和调用)。

 

 

6、监控SOAP消息

6.1、复制监控代码到工程

复制“org.apache.axis2.soapmonitor”包下的源代码文件到本工程下的src目录,并编译成classes文件。如下图所示:

6.2、复制classes文件到工程根目录

         复制“WEB-INF/classes/org.apache.axis2.soapmonitor”目录下编译好的classes文件到工程根目录“axis2test/WebContent”下。如下图所示:

        

6.3、配置axis2.xml

         在module节点后填加“<moduleref="soapmonitor"/>”,以打开监控。

   <!--================================================= -->

    <!--Global Modules  -->

    <!--================================================= -->

    <!--Comment this to disable Addressing -->

    <moduleref="addressing"/>

    <moduleref="soapmonitor"/>

 

 

6.4、配置web.xml

         填加监控servlet“SOAPMonitorService”,如下所示:

<servlet>

     <servlet-name>SOAPMonitorService</servlet-name>

     <display-name>SOAPMonitorService</display-name>              <servlet-class>org.apache.axis2.soapmonitor.servlet.SOAPMonitorService</servlet-class>

     <init-param>

          <param-name>SOAPMonitorPort</param-name>

          <param-value>5001</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>SOAPMonitorService</servlet-name>

<url-pattern>/SOAPMonitor</url-pattern>

</servlet-mapping>

 

6.5、开始监控

         打开“http://127.0.0.1:8080/axis2test/SOAPMonitor”连接进入监控页面;然后打开“http://127.0.0.1:8080/axis2test/services/Hello/say”访问WebService之后,回到监控页面即可看到请求与返回的消息内容。如下图所示:

        

 

7、文件传输测试

         以日志文件下载功能为类测试其传输能力。

7.1、环境

    Windows7、jdk1.5、eclipse3.5和axis1.5.1,jvm未优化。

7.2、设置

         (1)、Axis2WS-Client超时时间 60000 * 5 = 300000 ms ,即5 minutes;

         (2)、Eclipse jvm:launcher.XXMaxPermSize256M -Xms40m -Xmx512m

7.3、原始文件

transfer.files/services_monitor_json1000rows-459kb

7.4、后台输出消息

OMElement -elmt =

<fd:responsexmlns:fd="http://logserver.evensoft.org.cn/filesdata">

eyJyZXF1ZXN0SUQiOjAsInJl······

</fd:response>

文件保存位置 - path = d:/temp/test-download.log

文件下载结束(45691 / millis seconds)。

 

7.5、测试结果表:

文件大小

行数

执行时间

备注

456 KB

1000

45”69

45691 ms

920 KB

2000

2’33”

140365 ms

1.34 M

3000

7’38”

 

2.24 M

5000

 

 

612 M

135,2400

 

 

   注:程序设置传输超时默认值为5 minutes。

 

8、ZIP压缩测试

   文本文件压缩比率测试结果表:

文件大小

文件个数

原始大小

ZIP大小

11.2M

1

11.2M

88.3KB

11.2M

5

56M

336KB

11.2M

10

112M

672KB

612M

1

621M

3.56M

 

附录   名词解释

SOAP

Simple Object Access Protocol,简单文件传输协议。

From the draftW3C specification:

SOAP is a lightweight protocol for exchanging structured informationin a decentralized, distributed environment. It is an XML based protocol thatconsists of three parts: an envelope that defines a framework for describingwhat is in a message and how to process it, a set of encoding rules forexpressing instances of application-defined datatypes, and a convention forrepresenting remote procedure calls and responses.

SOAP是一种在分布式环境下实现结构化信息交换的轻量级协议。它是以XML为基础的协议包含三部分:首先封装定义一个结构其中信息的内容是什么及如何操作它们;然后为数据结构的类型展现实例设定编码规则;并且为远程请求与应答定义好协议。

AXIS

         ApacheEXtensible Interaction System 阿帕奇可扩展交互系统

  Axis本质上就是一个SOAP引擎,提供创建服务器端、客户端和网关SOAP操作的基本框架。

附录FAQ

命名空间错误

错误信息:

org.apache.axis2.AxisFault: namespacemismatch require http://logserver.evensoft.org.cn/filesdatafound http://example.org/filedata

处理:

修改AXIS“WEB-INF/services/LogFilesServices/META-INF/services.xml”配置文件,增加“<schemaschemaNamespace="http://logserver.esri.com/filesdata"/>”。

连接超时

文件传输测试超过3次,就会提示超时错误;信息如下:

org.apache.axis2.AxisFault: Timeout waiting for connection 
        atorg.apache.axis2.AxisFault.makeFault(AxisFault.java:430) 
        atorg.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:203

解决方案:

java客户端每次调用完成后,org.apache.axis2.client.ServiceClient对象一定要执行cleanupTransport()方法。C#客户也类似。

Server找不到Client

错误信息:

“ERROR:Theserver did not recognise the action which it received”

解决:

在Client端使用org.apache.axis2.client.Options里的setAction()方法,也就是把operationname设置进去。这样server端就能找到这个action了。

Action值在Server的WSDL描述表示为“getFilesList”或“download”。如下所示:

<wsdl:operation name="getFilesList">

              <http:operation location="LogFilesServices/getFilesList" />

<wsdl:input>

                           <mime:contenttype="text/xml" part="getFilesList"/>

                 </wsdl:input>

<wsdl:output>

                           <mime:contenttype="text/xml" part="getFilesList"/>

          </wsdl:output>

          </wsdl:operation>

<wsdl:operation name="download">

<http:operationlocation="LogFilesServices/download" />

<wsdl:input>

<mime:contenttype="text/xml" part="download" />

                 </wsdl:input>

<wsdl:output>

                           <mime:contenttype="text/xml" part="download"/>

                 </wsdl:output>

  </wsdl:operation>

 

ClassNotFoundException:AxisAdminServlet

错误信息:

java.lang.ClassNotFoundException: org.apache.axis2.transport.http.AxisAdminServlet

处理方法:

web.xml中此Servletpackage配置有误,正确的是:

“org.apache.axis2.webapp.AxisAdminServlet”。

截图:

NoClassDefFoundError:XMLStreamException

错误消息:

java.lang.NoClassDefFoundError:javax/xml/stream/XMLStreamException

处理方法:

         Copy所有jar文件到lib下即可,可能有的不是必须的,需要再验证。

如何修改axis2控制台默认密码?

         登录URL为“http://${localhost}:{port}/${WebServiceName}/axis2-admin/”的管理界面。默认的用户名为“admin”密码是“axis2”,实际部署开发中为了安全需要修改。

         修改方法是打开“${WEB_APP}\WEB-INF\conf\axis2.xml”找到如下节点内容自行修改即可:

<parametername="userName">admin</parameter>

<parameter name="password">axis2</parameter>

为何SOAPMonitor不能使用?

错误消息:

java.lang.ClassNotFoundException:

org.apache.axis2.soapmonitor.applet.SOAPMonitorApplet.class

解决:

默认“org.apache.axis2.soapmonitor”包下文件编译到了“WEB-INF/classes”目录下,而实际上要让SOAPMonitorapplet正常动行面需要这些classes文件放到web工程的要目录下,例如:“axis2test/org/apache/axis2/soapmonitor/”。

注:当然运行applet需要有JRE环境且浏览器打开Applet支持或安装插件。

 

 

 

原创粉丝点击