【问题标题】:Soap client throwing Unexpected element exception肥皂客户端抛出意外的元素异常
【发布时间】:2020-02-21 18:59:01
【问题描述】:

我有一个第 3 方 SOAP 服务。当我使用 POSTMAN 调用服务的端点时,它会将数据发回

POST - https://localhost:8443/api/PatronSearch

邮递员身体

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sch="http://www.myservice.com/api/schema">
<soapenv:Header/>
<soapenv:Body>
<sch:PatronSearch>
<sch:Patron>
<sch:LastName>Summers</sch:LastName> 
</sch:Patron>
</sch:PatronSearch>
</soapenv:Body>
</soapenv:Envelope>

POSTMAN 中的响应

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <PatronList xmlns:ns2="http://www.myservice.com/api" xmlns="http://www.myservice.com/api/schema">
            <Patron xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="EmployeeData">
                <Id>1</Id>
                <Type>Employee</Type>
                <ReadOnly>false</ReadOnly>
                <LastName>Summers</LastName>
                <FirstName>Petron</FirstName>
                <Gender>Male</Gender>
                <PhoneNo>+14567891234</PhoneNo>
                <Language>en</Language>
                <MobilePhoneNo>+14567891235</MobilePhoneNo>
                <Email>Summers.Petron@gmail.com</Email>
                <DepartmentId>2</DepartmentId>
            </Patron>
        </PatronList>
    </soap:Body>
</soap:Envelope>

但是当我尝试使用 java 访问相同的服务时,它会抛出以下错误

org.springframework.ws.soap.client.SoapFaultClientException: Unexpected element findPatron found.   Expected {http://www.myservice.com/api/schema}PatronSearch.

这是我调用此服务的方法

public PatronList getPatronInfo(PatronSearch patronSearch) {
        template = new WebServiceTemplate(marshaller);

        JAXBElement<PatronSearch> jaxbElement = new JAXBElement<PatronSearch>(new QName("", "findPatron"),
                PatronSearch.class, patronSearch);

        PatronList patronList = (PatronList) template
                .marshalSendAndReceive("https://localhost:8443/api/PatronSearch", jaxbElement);

        return patronList;
    }

这是wsdl的快照

    <?xml version="1.0" encoding="utf-8"?>
<!--Created with Liquid XML Studio Developer Edition 9.1.11.3570 (http://www.liquid-technologies.com)-->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://example.com/" xmlns:ns="http://www.myservice.com/api/schema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" name="myservice" targetNamespace="http://www.myservice.com/api" xmlns="http://schemas.xmlsoap.org/wsdl/">
    <types>
        <xs:schema xmlns:tns="http://schemas.xmlsoap.org/wsdl/">
            <xs:import schemaLocation="MyServiceWebService.xsd" namespace="www.myservice.com/api/schema" />
        </xs:schema>
    </types>
    <message name="PatronSearch">
        <part xmlns:q1="http://www.myservice.com/api/schema" name="search" element="q1:PatronSearch" />
    </message>
    <message name="PatronList">
        <part xmlns:q1="http://www.myservice.com/api/schema" name="list" element="q1:PatronList" />
    </message>
    <portType name="MyServiceWebServiceType">
    <operation name="findPatron">
            <input xmlns:q1="http://www.myservice.com/api" name="findPatron" message="q1:PatronSearch" />
            <output xmlns:q1="http://www.myservice.com/api" name="listPatron" message="q1:PatronList" />
            <fault xmlns:q1="http://www.myservice.com/api" name="fout" message="q1:Fault" />
        </operation>
        </portType>

        <binding xmlns:q1="http://www.myservice.com/api/" name="MyServiceWebServiceSoapBinding" type="q1:MyServiceWebServiceType">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
        <operation name="findPatron">
            <input name="findPatron" />
            <output name="listPatron" />
            <fault name="fout" />
        </operation>
        </binding>
    <service name="MyServiceWebService">
        <port xmlns:q1="http://www.myservice.com/api" name="MyServiceWebServicePort" binding="q1:MyServiceWebServiceSoapBinding">
            <soap:address location="https://localhost:8443/api" />
        </port>
    </service>
</definitions>

任何建议我做错了什么或更好的方式来调用服务。

【问题讨论】:

    标签: java web-services wsdl soap-client


    【解决方案1】:

    错误消息指出:“Expected {http://www.myservice.com/api/schema}PatronSearch”。在构造 QName 时提供适当的命名空间和本地名称:

    JAXBElement<PatronSearch> jaxbElement = new JAXBElement<PatronSearch>(new QName("http://www.myservice.com/api/schema", "PatronSearch"), PatronSearch.class, patronSearch);
    

    【讨论】:

    • 感谢您的回复,我试了一下并收到此错误“意外元素 {myservice.com/api/schema}PatronSearch 找到。预期 {myservice.com/api/schema}VersionInfo。”
    • 您能否记录编组的 PatronSearch 实例并附加输出?像这样的东西:marshaller.marshal(patronSearch, System.out); 看看 MyServiceWebService.xsd 也可能会有所帮助
    • 我正在创建一个空的 PatronSerch 实例,例如 - PatronSearch PatronSearch = new PatronSearch();并将此实例传递给方法。顺便说一句,没有将 PrintStream 作为参数的 marshal 方法
    • 抱歉,如果不了解架构/消息结构,只能判断验证失败。
    猜你喜欢
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    • 2017-09-27
    相关资源
    最近更新 更多