【问题标题】:Struggling to generate a SOAP-Service based on wsdl file by using cxf努力使用 cxf 生成基于 wsdl 文件的 SOAP-Service
【发布时间】:2019-01-17 10:40:44
【问题描述】:

我正在努力从 wsdl 文件中使用 Apache cxf 创建一个有效的 SOAP 服务。我正在访问该服务,但已经收到与您在最后一个代码 sn-p 中看到的相同的异常消息。我向您展示了更多的代码 sn-ps,因为我对这种创建 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>

【问题讨论】:

标签: xml soap wsdl cxf wsdl2java


【解决方案1】:

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

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

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

2) 违反了我的 wsdl 文件中使用 cxf 所需的一些约定:

必须将消息部分元素 DataReqDataRsp 替换为 Data 用于请求和 DataResponse 用于响应.

3) 执行 cxf-command 没问题:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多