在上两篇写WebService的文章中:
      1)使用XFire+Spring构建Web Service(一)——helloWorld篇
      2)使用XFire+Spring构建Web Service(二)
      讲到了如何使用XFire+Spring构建WebService,并生成了对应的wsdl文件。针对这两篇文章,收到很多留言和读者来信,大部分都是针对拿到wsdl后,如何根据wsdl生成对应的Java代码,并进行测试。
      基于在开发过程中,很多时候都是拿到另一方的wsdl,需要与该方进行联调测试,所以在本文中,我大概的整理了一下,希望能帮读者解决一些问题。
 第一章. 准备wsdl
      这阵子正好在写几个wsdl,就拿其中一个来做实例吧。
      首先贴下接口协议文档:
      []
第二章 实例wsdl
  相关的wsdl有5个,其中两个是各个接口共用的,定义了共用的异常和数据对象之类的东西。
  1)acd_common_faults.wsdl:定义了共用的异常ServiceException。
<?xml version="1.0" encoding="UTF-8"?>
<!-- 编辑使用 XMLSpy v2006 U (http://www.altova.com) 由 any (any) -->
<!-- 2009.10.29 -->
<wsdl:definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:acd_common_faults_xsd="http://cintel.com/ws/acd/system/v1_0/common/model" targetNamespace="http://www.cintel.com.cn/ws/acd/system/v1_0/common/faults" name="common_faults">
    <wsdl:types>
        <xsd:schema elementFormDefault="qualified">
            <xsd:import namespace="http://cintel.com/ws/acd/system/v1_0/common/model" schemaLocation="acd_common_types.xsd"/>
        </xsd:schema>
    </wsdl:types>
    <wsdl:message name="ServiceException">
        <wsdl:part name="ServiceException" element="acd_common_faults_xsd:ServiceException"/>
    </wsdl:message>
</wsdl:definitions>

  2)acd_common_types.xsd:定义了共用的数据类型


  3)acd_accessCode_types.xsd:定义了接入码的数据对象 
<?xml version="1.0" encoding="UTF-8"?>
<!-- 20090618 -->
<xsd:schema xmlns:acd_common_xsd="http://cintel.com/ws/acd/system/v1_0/common/model" xmlns:acd_accessCode_xsd="http://cintel.com/ws/acd/system/v1_0/accessCode/model" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://cintel.com/ws/acd/system/v1_0/accessCode/model">
    <xsd:element name="AccessCodeContext" type="acd_accessCode_xsd:AccessCodeContext"/>
    <xsd:complexType name="AccessCodeContext">
        <xsd:sequence>
            <xsd:element name="accessCode" type="xsd:string" nillable="false"/>
            <xsd:element name="vccId" type="xsd:string" nillable="true"/>
            <xsd:element name="routeStrategy" type="xsd:int" nillable="false"/>
            <xsd:element name="telephoneNumber" type="xsd:string" nillable="true"/>
            <xsd:element name="serviceId" type="xsd:int" nillable="true"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

  4)acd_accessCode_service.wsdl:定义了接入码管理的各个方法
<?xml version="1.0" encoding="UTF-8"?>
<!-- 2009-10-29 -->
<wsdl:definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://cintel.com/ws/acd/system/v1_0/accessCode/service" xmlns:interface="http://cintel.com/ws/acd/system/v1_0/accessCode/interface" targetNamespace="http://cintel.com/ws/acd/system/v1_0/accessCode/service" name="acd_accessCode_service">
    <wsdl:import namespace="http://cintel.com/ws/acd/system/v1_0/accessCode/interface" location="acd_accessCode_interface.wsdl"/>
    <wsdl:binding name="AccessCodeBinding" type="interface:AccessCode">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="getTotalItem">
            <soap:operation style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
            <wsdl:fault name="ServiceException">
                <soap:fault name="ServiceException" use="literal"/>
            </wsdl:fault>
        </wsdl:operation>
        <wsdl:operation name="list">
            <soap:operation style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
            <wsdl:fault name="ServiceException">
                <soap:fault name="ServiceException" use="literal"/>
            </wsdl:fault>
        </wsdl:operation>
        <wsdl:operation name="find">
            <soap:operation style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
            <wsdl:fault name="ServiceException">
                <soap:fault name="ServiceException" use="literal"/>
            </wsdl:fault>
        </wsdl:operation>
        <wsdl:operation name="save">
            <soap:operation style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
            <wsdl:fault name="ServiceException">
                <soap:fault name="ServiceException" use="literal"/>
            </wsdl:fault>
        </wsdl:operation>
        <wsdl:operation name="update">
            <soap:operation style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
            <wsdl:fault name="ServiceException">
                <soap:fault name="ServiceException" use="literal"/>
            </wsdl:fault>
        </wsdl:operation>
        <wsdl:operation name="delete">
            <soap:operation style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
            <wsdl:fault name="ServiceException">
                <soap:fault name="ServiceException" use="literal"/>
            </wsdl:fault>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="AcessCodeService">
        <wsdl:port name="AccessCode" binding="tns:AccessCodeBinding">
            <soap:address location="http://localhost:8686/AccessCodeService/services/AccessCode"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>
  5)acd_accessCode_interface.wsdl:接口定义
<?xml version="1.0" encoding="UTF-8"?>
<!--2009-10-29 -->
<wsdl:definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:acd_accessCode="http://cintel.com/ws/acd/system/v1_0/accessCode/interface" xmlns:acd_common_xsd="http://cintel.com/ws/acd/system/v1_0/common/model" xmlns:acd_common_faults="http://www.cintel.com.cn/ws/acd/system/v1_0/common/faults" xmlns:acd_accessCode_xsd="http://cintel.com/ws/acd/system/v1_0/accessCode/model" xmlns:acd_accessCode_local_xsd="http://cintel.com/ws/acd/system/v1_0/accessCode/local" targetNamespace="http://cintel.com/ws/acd/system/v1_0/accessCode/interface" name="acd_AccessCode_interface">
    <wsdl:import namespace="http://www.cintel.com.cn/ws/acd/system/v1_0/common/faults" location="acd_common_faults.wsdl"/>
    <wsdl:types>
        <xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://cintel.com/ws/acd/system/v1_0/accessCode/local">
            <xsd:import namespace="http://cintel.com/ws/acd/system/v1_0/accessCode/model" schemaLocation="acd_accessCode_types.xsd"/>
            <xsd:import namespace="http://cintel.com/ws/acd/system/v1_0/common/model" schemaLocation="acd_common_types.xsd"/>
            <xsd:element name="getTotalItem" type="acd_accessCode_local_xsd:getTotalItem"/>
            <xsd:complexType name="getTotalItem">
                <xsd:sequence>
                    <xsd:element name="accessCodeContext" type="acd_accessCode_xsd:AccessCodeContext"/>
                </xsd:sequence>
            </xsd:complexType>
            <xsd:element name="getTotalItemResponse" type="acd_accessCode_local_xsd:getTotalItemResponse"/>
            <xsd:complexType name="getTotalItemResponse">
                <xsd:sequence>
                    <xsd:element name="totalItem" type="xsd:int" nillable="false"/>
                </xsd:sequence>
            </xsd:complexType>
            <xsd:element name="list" type="acd_accessCode_local_xsd:list"/>
            <xsd:complexType name="list">
                <xsd:sequence>
                    <xsd:element name="startItem" type="xsd:int" nillable="false"/>
                    <xsd:element name="maxItem" type="xsd:int" nillable="false"/>
                    <xsd:element name="accessCodeContext" type="acd_accessCode_xsd:AccessCodeContext"/>
                </xsd:sequence>
            </xsd:complexType>
            <xsd:element name="listResponse" type="acd_accessCode_local_xsd:listResponse"/>
            <xsd:complexType name="listResponse">
                <xsd:sequence>
                    <xsd:element name="accessCodeContextArray" type="acd_accessCode_xsd:AccessCodeContext" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
                </xsd:sequence>
            </xsd:complexType>
            <xsd:element name="find" type="acd_accessCode_local_xsd:find"/>
            <xsd:complexType name="find">
                <xsd:sequence>
                    <xsd:element name="accessCode" type="xsd:string"/>
                </xsd:sequence>
            </xsd:complexType>
            <xsd:element name="findResponse" type="acd_accessCode_local_xsd:findResponse"/>
            <xsd:complexType name="findResponse">
                <xsd:sequence>
                    <xsd:element name="accessCodeContext" type="acd_accessCode_xsd:AccessCodeContext"/>
                </xsd:sequence>
            </xsd:complexType>
            <xsd:element name="save" type="acd_accessCode_local_xsd:save"/>
            <xsd:complexType name="save">
                <xsd:sequence>
                    <xsd:element name="accessCodeContext" type="acd_accessCode_xsd:AccessCodeContext"/>
                </xsd:sequence>
            </xsd:complexType>
            <xsd:element name="saveResponse" type="acd_accessCode_local_xsd:saveResponse"/>
            <xsd:complexType name="saveResponse">
                <xsd:sequence>
                    <xsd:element name="result" type="xsd:boolean"/>
                </xsd:sequence>
            </xsd:complexType>
            <xsd:element name="update" type="acd_accessCode_local_xsd:update"/>
            <xsd:complexType name="update">
                <xsd:sequence>
                    <xsd:element name="accessCodeContext" type="acd_accessCode_xsd:AccessCodeContext"/>
                </xsd:sequence>
            </xsd:complexType>
            <xsd:element name="updateResponse" type="acd_accessCode_local_xsd:updateResponse"/>
            <xsd:complexType name="updateResponse">
                <xsd:sequence>
                    <xsd:element name="result" type="xsd:boolean"/>
                </xsd:sequence>
            </xsd:complexType>
            <xsd:element name="delete" type="acd_accessCode_local_xsd:delete"/>
            <xsd:complexType name="delete">
                <xsd:sequence>
                    <xsd:element name="accessCode" type="xsd:string"/>
                </xsd:sequence>
            </xsd:complexType>
            <xsd:element name="deleteResponse" type="acd_accessCode_local_xsd:deleteResponse"/>
            <xsd:complexType name="deleteResponse">
                <xsd:sequence>
                    <xsd:element name="result" type="xsd:boolean"/>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:schema>
    </wsdl:types>
    <wsdl:message name="AccessCode_getTotalItemMessageRequest">
        <wsdl:part name="parameters" element="acd_accessCode_local_xsd:getTotalItem"/>
    </wsdl:message>
    <wsdl:message name="AccessCode_getTotalItemMessageResponse">
        <wsdl:part name="result" element="acd_accessCode_local_xsd:getTotalItemResponse"/>
    </wsdl:message>
    <wsdl:message name="AccessCode_listMessageRequest">
        <wsdl:part name="parameters" element="acd_accessCode_local_xsd:list"/>
    </wsdl:message>
    <wsdl:message name="AccessCode_listMessageResponse">
        <wsdl:part name="result" element="acd_accessCode_local_xsd:listResponse"/>
    </wsdl:message>
    <wsdl:message name="AccessCode_findMessageRequest">
        <wsdl:part name="parameters" element="acd_accessCode_local_xsd:find"/>
    </wsdl:message>
    <wsdl:message name="AccessCode_findMessageResponse">
        <wsdl:part name="result" element="acd_accessCode_local_xsd:findResponse"/>
    </wsdl:message>
    <wsdl:message name="AccessCode_saveMessageRequest">
        <wsdl:part name="parameters" element="acd_accessCode_local_xsd:save"/>
    </wsdl:message>
    <wsdl:message name="AccessCode_saveMessageResponse">
        <wsdl:part name="result" element="acd_accessCode_local_xsd:saveResponse"/>
    </wsdl:message>
    <wsdl:message name="AccessCode_updateMessageRequest">
        <wsdl:part name="parameters" element="acd_accessCode_local_xsd:update"/>
    </wsdl:message>
    <wsdl:message name="AccessCode_updateMessageResponse">
        <wsdl:part name="result" element="acd_accessCode_local_xsd:updateResponse"/>
    </wsdl:message>
    <wsdl:message name="AccessCode_deleteMessageRequest">
        <wsdl:part name="parameters" element="acd_accessCode_local_xsd:delete"/>
    </wsdl:message>
    <wsdl:message name="AccessCode_deleteMessageResponse">
        <wsdl:part name="result" element="acd_accessCode_local_xsd:deleteResponse"/>
    </wsdl:message>
    <wsdl:portType name="AccessCode">
        <wsdl:operation name="getTotalItem">
            <wsdl:input message="acd_accessCode:AccessCode_getTotalItemMessageRequest"/>
            <wsdl:output message="acd_accessCode:AccessCode_getTotalItemMessageResponse"/>
            <wsdl:fault name="ServiceException" message="acd_common_faults:ServiceException"/>
        </wsdl:operation>
        <wsdl:operation name="list">
            <wsdl:input message="acd_accessCode:AccessCode_listMessageRequest"/>
            <wsdl:output message="acd_accessCode:AccessCode_listMessageResponse"/>
            <wsdl:fault name="ServiceException" message="acd_common_faults:ServiceException"/>
        </wsdl:operation>
        <wsdl:operation name="find">
            <wsdl:input message="acd_accessCode:AccessCode_findMessageRequest"/>
            <wsdl:output message="acd_accessCode:AccessCode_findMessageResponse"/>
            <wsdl:fault name="ServiceException" message="acd_common_faults:ServiceException"/>
        </wsdl:operation>
        <wsdl:operation name="save">
            <wsdl:input message="acd_accessCode:AccessCode_saveMessageRequest"/>
            <wsdl:output message="acd_accessCode:AccessCode_saveMessageResponse"/>
            <wsdl:fault name="ServiceException" message="acd_common_faults:ServiceException"/>
        </wsdl:operation>
        <wsdl:operation name="update">
            <wsdl:input message="acd_accessCode:AccessCode_updateMessageRequest"/>
            <wsdl:output message="acd_accessCode:AccessCode_updateMessageResponse"/>
            <wsdl:fault name="ServiceException" message="acd_common_faults:ServiceException"/>
        </wsdl:operation>
        <wsdl:operation name="delete">
            <wsdl:input message="acd_accessCode:AccessCode_deleteMessageRequest"/>
            <wsdl:output message="acd_accessCode:AccessCode_deleteMessageResponse"/>
            <wsdl:fault name="ServiceException" message="acd_common_faults:ServiceException"/>
        </wsdl:operation>
    </wsdl:portType>
</wsdl:definitions>

 

第三章 在MyEclispe中通过wsdl生成Java代码
   

3.1   建立工程环境

MyEclipse中建立一个工程,例如建立一个名为wsdl2javaWEB工程。需要添加到WEB-INF/lib目录下的包如下所示:
  

wsdl目录拷贝到工程目录,例如src/wsdl目录下。

3. 2.   根据wsdl文件生成对应的java


选择新建的工程后,点击右键,选择“
Run As->Run”,如下图所示:


弹出窗口如下图所示:

   在上图中,左边选择“Java Application”后,点击左上角的新增“”按钮new configuration,页面如下图所示:
   

   在上图中,将“Include libraries when searching for a main class 勾上,而后点击“Main class”右边的“Search”按钮,弹出窗口如下图所示:
   

   在上图中输入WSDL2Java后,查找到axis这个jar包中对应类,点击“OK”按钮,此时主窗口如下图所示:
   

   在上图中点击“Arguments”页签,Arguments里把要生成的web service URIwsdl文件写进去,最后点run。在下图中输入的是wsdl文件的路径:     src/wsdl/acd_accessCode_interface.wsdl、src/wsdl/acd_accessCode_service.wsdl

   
   生成的代码的结构如下图所示:
  

第四章 更简便的生成Java代码的方法
    本章讲述的方法是笔者采用的方法,也是笔者所推荐的,原理与前一章相同,但是要简便一些。
4.1 准备jar包
    将上一章提到的6个jar包放到某个目录,例如E:\amigo\project\ws_lib
4.2 拷贝wsdl以及相关文件
    将上面章节5个wsdl文件拷贝到某个目录,例如:E:\interface
4.3 编写一个批处理文件
    编写一个根据wsdl生成Java代码的批处理文件,以后都可以拿来用。在wsdl文件所在目录创建批处理文件createPackageByWsdl.bat,编写该文件内容:
   
set LibPath=E:\amigo\project\ws_lib

set AxisClassPath=%LibPath%\axis.jar;%LibPath%\commons-logging.jar;%LibPath%\commons-discovery-0.2.jar;%LibPath%\jaxrpc.jar;%LibPath%\saaj.jar;%LibPath%\wsdl4j.jar;

set WSDL2JavaCommand=java -classpath %AxisClassPath% org.apache.axis.wsdl.WSDL2Java
set WsDeploy=-s


%WSDL2JavaCommand% %WsDeploy% acd_accessCode_interface.wsdl
%WSDL2JavaCommand% %WsDeploy% acd_accessCode_service.wsdl
   在后面可加入更多的wsdl文件。
   编写完成后,运行该批处理文件,可在同目录下生成指定的wsdl对应的Java类。生成的文件与上一章的文件一样。
 
注: 
posted on 2012-05-25 19:39  老杨HJ  阅读(5617)  评论(0编辑  收藏  举报