使用cxf努力基于wsdl文件生成SOAP服务

时间:2019-01-17 10:40:45

标签: xml soap wsdl cxf wsdl2java

我正在努力从wsdl-File用Apache cxf创建可工作的SOAP服务。我正在到达该服务,但是已经收到与上一个代码片段相同的异常消息。我向您展示了更多代码片段,因为我是这种创建Web服务的新手,并且不知道我的错误在哪里。 感谢您的帮助!

从我的wsdl文件开始:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="DataService" targetNamespace="http://service.data.com"
              xmlns:xsd="http://www.w3.org/2001/XMLSchema"
              xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
              xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
              xmlns:test="http://service.data.com">

<wsdl:types>
    <xsd:schema targetNamespace="http://service.data.com"
                xmlns="http://www.w3.org/2001/XMLSchema"
                elementFormDefault="qualified">
        <element name="DataReq" type="test:DataReqType"/>
        <element name="DataRsp" type="test:DataRspType"/>

        <complexType name="DataReqType">
            <sequence>
                <element minOccurs="0" name="DataXML" nillable="true" type="test:DataXML"/>
            </sequence>
        </complexType>
        <complexType name="DataRspType">
            <sequence>
                <element name="DataError" type="test:DataError"/>
                <element name="success" nillable="true" type="int"/>
            </sequence>
        </complexType>

        <complexType name="DataXML">
            <sequence>
                <element name="xmlString" type="test:String200"/>
            </sequence>
        </complexType>
        <complexType name="DataError">
            <sequence>
                <element name="errorCode" nillable="false" type="int"/>
                <element name="errorMessage" nillable="true" type="test:String40"/>
            </sequence>
        </complexType>

        <simpleType name="String40">
            <restriction base="string">
                <maxLength value="40"/>
            </restriction>
        </simpleType>
        <simpleType name="String200">
            <restriction base="string">
                <maxLength value="200"/>
            </restriction>
        </simpleType>

    </xsd:schema>
</wsdl:types>

<wsdl:message name="PostDataReq">
    <wsdl:part name="body" element="test:DataReq"/>
</wsdl:message>
<wsdl:message name="PostDataRsp">
    <wsdl:part name="body" element="test:DataRsp"/>
</wsdl:message>

<wsdl:portType name="DataPortType">
    <wsdl:operation name="dataImport">
        <wsdl:input message="test:PostDataReq"/>
        <wsdl:output message="test:PostDataRsp"/>
    </wsdl:operation>
</wsdl:portType>

<wsdl:binding name="DataSOAPBinding" type="test:DataPortType">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="dataImport">
        <soap:operation soapAction="http://service.data.com/dataImport"/>
        <wsdl:input>
            <soap:body use="literal"/>
        </wsdl:input>
        <wsdl:output>
            <soap:body use="literal"/>
        </wsdl:output>
    </wsdl:operation>
</wsdl:binding>

<wsdl:service name="DataService">
    <wsdl:port binding="test:DataSOAPBinding" name="DataServicePort">
        <soap:address location="http://localhost:32080/test/services/dataImport"/>
    </wsdl:port>
</wsdl:service>

我的cxf-servlet.xml和用于托管服务的bean:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jaxws="http://cxf.apache.org/jaxws"
   xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://cxf.apache.org/jaxws
         http://cxf.apache.org/schemas/jaxws.xsd">

<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

<jaxws:endpoint id="DataService"
                implementor="com.test.DataService"
                address="/dataImport">
</jaxws:endpoint>

在浏览器中请求服务时得到的结果添加了“?wsdl”:

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://service.data.com"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:ns2="http://schemas.xmlsoap.org/soap/http"
xmlns:ns1="http://data.server.test.com/" name="DataService"
targetNamespace="http://service.data.com">
<wsdl:import location="http://localhost:32080/dataImport?wsdl=DataService.wsdl"
 namespace="http://data.server.test.com/"> </wsdl:import>
 <wsdl:binding name="DataServiceSoapBinding" type="ns1:DataService">
 <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
</wsdl:binding>
<wsdl:service name="DataService">
    <wsdl:port binding="tns:DataServiceSoapBinding" name="DataServicePort">
        <soap:address location="http://localhost:32080/dataImport"/>
    </wsdl:port>
 </wsdl:service>
</wsdl:definitions>

我正在使用SOAP-UI进行后期处理:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ser="http://service.data.com">
<soapenv:Header/>
<soapenv:Body>
    <ser:DataReq>
        <ser:DataXML>
            <ser:xmlString>Test</ser:xmlString>
        </ser:DataXML>
    </ser:DataReq>
</soapenv:Body>
</soapenv:Envelope>

它响应并带有以下异常:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
    <soap:Fault>
        <faultcode>soap:Client</faultcode>
        <faultstring>Message part {http://service.data.com}DataReq was not recognized.
            (Does it exist in service WSDL?)</faultstring>
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

1 个答案:

答案 0 :(得分:0)

我通过从头开始一个新的独立项目找到了解决方案。我在代码中犯了几个错误。

1)在cxf-servlet.xml中,我必须校准生成的 DataPortImpl ,而不是 DataService

<jaxws:endpoint id="DataService"
                implementor="com.test.DataPortImpl"
                address="/dataImport">
</jaxws:endpoint>

2)违反了我的wsdl文件中使用cxf所必需的某些内容:

必须将 DataReq DataRsp 的消息部分元素替换为 Data (用于请求)和 DataResponse (用于响应)

3)执行cxf命令很好:

wsdl2java -server -impl  -d %pathToDirectory% -p %pathToPackage%
-wsdlLocation classpath:%relativePathToWSDL% %pathToWSDL%