SOAP与AXIS2入门教程(附带实例)

来源:互联网 发布:淘宝c店保证金怎么办 编辑:程序博客网 时间:2024/05/19 20:39

最近项目需要用到SOAP以及AXIS2的知识,在学习之余,将第一天学到的内容整理了一下,一来做为笔记做个记录,二来如果有需要的,可以做为参考,今天主要是完成了一下功能,通过一个SOAP请求消息(可以自己构造也可以通过指定一个xml文件),然后在Web Service中获取这个SOAP请求消息(一个OMElement对象),通过解析这个对象,获取需要的信息,然后对这些信息进行业务处理,最后返回一个SOAP响应消息。

 

获取AXIS2 1.1,由于做项目一般使用稳定的发布版本,所以本文没有使用最新的AXIS2 1.1.1版本,该版本可以从Apache官方网站下载。下载地址如下: http://ws.apache.org/axis2/download/1_1/download.cgi 在上面连接中,有三个版本的axis2可以供下载,分别是:Standard Binary Distribution,Source Distribution,WAR (Web Archive) Distribution,其中标准版可以直接独立使用(Stand-alone),源代码版本需要使用maven进行构建,同时允许开发人员自己修改源代码,本文使用的是WAR版本,可以直接发布在WEB容器中(本文使用的是Tomcat5.5.17)。在上面的链接中还有一个是DOCS的下载,最好一并下载,docs中包含用户手册,快速上手指南,以及其他相关文档,对了解并熟悉AXIS2很有帮助。

 

闲话少说,将已经下载的WAR包发布(拷贝+粘贴)到%TOMCAT_HOME%/webapps/目录下,其中%TOMCAT_HOME%以Tomcat安装目录进行替换。启动Tomcat,在IE地址栏中键入:http://localhost:8080/axis2,如果部署成功,即可看到欢迎页面,一般情况下不会出现什么错误。进入主页之后,可以通过进入Administration链接来管理WEB服务,其中初始化用户名和密码分别为admin/axis2。一般情况下,在管理控制台下进行操作比较方便,但是本文将直接进行操作系统目录级的操作(不会使用UploadServices进行发布服务,而是直接将*.aar拷贝到%TOMCAT_HOME%/webapps/axis2/WEB-INF/services目录下)。

 

下面我们建立自定义的WEB服务:我们要将类似以下XML格式的SOAP请求转换为SOAP响应,并获取SOAP请求中的关键元素,进行业务操作,本例子中只是简单的将数值拷贝,没有进行实际的业务操作,但是在代码中进行了指示: SOAP请求:

ABC DEF GHI SOAP响应:

GHI 在实际应用中,我们可能要获取ABC,DEF,GHI来进行业务处理,最后返回一个其他的消息,但是在示例中,我们仅仅获取ABC,DEF,GHI,然后打印出来,并返回GHI。

 

Step1.建立工程并创建文件建立如下图所示的JAVA工程(工程的源代码路径与编译后路径不同,源代码为project/src,编译后为project/bin):首先建立一些类,然后创建serives.xml,最后建立build.xml文件(ANT的构建文件,为了方便测试与构建,所以使用了ANT)目录结构

SDES_Enhance

|src

| |newsdes.support.service

| | |RevokeService.java

| |META-INF

| | |services.xml

|build.xml

 

下面是这些文件的内容:

//RevokeService.java(这个是进行WEB服务的类):

package newsdes.support.service;

 

import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.xml.namespace.QName; 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 RevokeService { /** * The request soap message object. */ public static OMElement requestSoap = null; /** * Write the soap response message to xml file. * * @param res * The resource of that forms the xml file. * @param filePath * The path where the xml file be stored. */ private static void writeResponse(String res, String filePath) { try { FileOutputStream fos = new FileOutputStream(filePath); byte[] bytes = res.getBytes(); fos.write(bytes); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * Generate the path where the xml file is stored. * * @param fileName * @return */ private static String makePath(String fileName) { String path = "C:/eclipse/workspace/SDES_Enhance/data/" + fileName; return path; } //主要的WEB服务方法 public OMElement RevokeCertRequest(OMElement soapBody) { requestSoap = soapBody; QName issuerName = new QName("Issuer"); QName serialName = new QName("Serial"); QName revocationDateName = new QName("RevocationDate"); OMElement issuerElement = requestSoap.getFirstChildWithName(issuerName); OMElement serialElement = requestSoap.getFirstChildWithName(serialName); OMElement revocationDateElement = requestSoap .getFirstChildWithName(revocationDateName); String issuer = issuerElement.getText(); String serial = serialElement.getText(); String revocationDate = revocationDateElement.getText(); // print out the value System.out.println(issuer); System.out.println(serial); System.out.println(revocationDate); // TODO use "issuer,serial,revocationDate" to do business // Generate the soap response message OMFactory soapFactory = OMAbstractFactory.getOMFactory(); OMNamespace omNs = soapFactory.createOMNamespace( "http://www.sdes.net/", ""); OMElement soapResponse = soapFactory.createOMElement( "RevokeCertResponse", omNs); OMElement soapMain = soapFactory.createOMElement("RevokeDate", omNs); soapMain.setText(revocationDate); soapResponse.addChild(soapMain); soapResponse.build(); String path = makePath("resMsg.xml"); writeResponse(soapResponse.toString(), path); return soapResponse; } }

 

该Service类主要实现了将传入的参数OMElement进行解析,获取其中的Issuer,Serial以及RevocationDate元素的值,然后进行业务处理(蓝色部分,省略)。然后根据业务操作结果,将响应返回。(这里仅仅是将内容写入到一个XML文件中)。

 

services.xml(WEB服务的配置文件,放置在META-INF目录下):

<serviceGroup>
      <service name="CertRevokeService">
           <description>
                 This is the service for revoking certificate.
           </description>
           <parameter name="ServiceClass" locked="false">
                  newsdes.support.service.RevokeService
           </parameter>
           <operation name="RevokeCertRequest">
                 <messageReceiver
                      class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />
                 <actionMapping>urn:RevokeCertRequest</actionMapping>
           </operation>
      </service>
</serviceGroup>

 

This is the service for revoking certificate. newsdes.support.service.RevokeService urn:RevokeCertRequest 其中serviceGroup中可以包含多个service,如果只有一个的时候,可以省略外层的serviceGroup元素。服务名为CertRevokeService。主要业务操作为RevokeCertRequest。红色部分标志了进行WEB服务的主要类。

 

Step2:进行打包首先编写build.xml文件,为了简便(只是个例子,所以任务也比较简单,只是将project/bin目录下的内容进行JAR打包,文件名为SDES_Enhance.aar,其中.aar为AXIS2的应用的后缀名。)

<project name = "SDES_Enhance" default="deploy" basedir=".">
      <description>
            Deploy SDES_Enhance Services
    </description>
      <property name="dist" value="${basedir}/dist"/>
      <property name="service" value="D:/Program Files/apache-tomcat-6.0.26/webapps/axis2/WEB-INF/services"/>
      <target name="init">
           <echo>Initializing the environment!</echo>
           <delete dir="${dist}"/>
           <delete dir="${basedir}/data"/>
           <mkdir dir="${dist}"/>
           <mkdir dir="${basedir}/data"/>
      </target>
      <target name="jar" depends="init">
           <echo>Compressing files to .aar file!</echo>
           <jar basedir="${basedir}/bin" destfile="${dist}/SDES_Enhance.aar"/>
      </target>
      <target name="deploy" depends="jar">
           <echo>Deploying service!</echo>
           <copy todir="${service}">
                 <fileset dir="${dist}">
                      <include name="SDES_Enhance.aar"/>
                 </fileset>
           </copy>
      </target>
</project>

Deploy SDES_Enhance Services Initializing the environment! Compressing files to .aar file! Deploying service! 打包完成后,直接将SDES_Enhance.aar部署到 %TOMCAT_HOME%/webapps/axis2/WEB-INF/services目录下,在TOMCAT启动情况下,会进行HOT-DEPLOY,直接部署完成,在http://localhost:8080/axis2/axis2-admin/listService下刷新,可以看到部署的服务:CertRevokeService。如下图所示:

 

 

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/daryl715/archive/2007/05/09/1602283.aspx

---另附eclipse 中ant打包快速上手指南

http://www.weste.net/2004/12-3/09122275829.html

 

原创粉丝点击