使用此WSDL时,为什么我的SOAP请求始终为空?

时间:2019-06-21 12:11:10

标签: java spring spring-boot soap wsdl

我正在使用Spring Boot构建SOAP Web服务。第三方提供了一个.wsdl文件,我正在用wsdl2java生成Java类。

在使用SoapUI进行测试时,我可以点击EndPoint。但是,该请求始终为空,除非我在SoapUI中手动修改XML请求。

这是wsdl

<definitions name="Employee"
             targetNamespace="http://example.com/Employee.wsdl"
             xmlns:tns="http://example.com/Employee.wsdl"
             xmlns:tns0="http://example.com/Employee.wsdl/types/"
             xmlns="http://schemas.xmlsoap.org/wsdl/"
             xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
             xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <types>
        <schema targetNamespace="http://example.com/Employee.wsdl/types/"
                xmlns:tns="http://example.com/Employee.wsdl/types/"
                elementFormDefault="qualified"
                xmlns="http://www.w3.org/2001/XMLSchema"
                xmlns:soap11-enc="http://schemas.xmlsoap.org/soap/encoding/"
                xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
            <complexType name="EmployeeIdRecUserArray">
                <sequence>
                    <element name="EmployeeIdRecUser" type="tns:EmployeeIdRecUser" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
                </sequence>
            </complexType>
            <complexType name="EmployeeIdRecUser">
                <complexContent>
                    <extension base="tns:EmplyeeIdParRecBase">
                        <sequence>
                            <element name="employeeId" type="string" nillable="true"/>
                        </sequence>
                    </extension>
                </complexContent>
            </complexType>
            <complexType name="EmplyeeIdParRecBase">
                <sequence/>
            </complexType>
            <complexType name="EmployeeDetailRecUserArray">
                <sequence>
                    <element name="EmployeeFinDetailRecUser" type="tns:EmployeeFinDetailRecUser" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
                </sequence>
            </complexType>
            <complexType name="EmployeeFinDetailRecUser">
                <complexContent>
                    <extension base="tns:EmployeeDetailRecBase">
                        <sequence>
                            <element name="employeeId" type="string" nillable="true"/>
                            <element name="firstName" type="string" nillable="true"/>
                            <element name="lastName" type="string" nillable="true"/>
                            <element name="mailingAddress" type="string" nillable="true"/>
                        </sequence>
                    </extension>
                </complexContent>
            </complexType>
            <complexType name="EmployeeDetailRecBase">
                <sequence/>
            </complexType>
        </schema>
    </types>
    <message name="getEmployeeDetails">
        <wsdl:part name="employeeIdTab" type="tns0:EmployeeIdRecUserArray"/>
    </message>
    <message name="getEmployeeDetailsResponse">
        <wsdl:part name="result" type="tns0:EmployeeDetailRecUserArray"/>
    </message>
    <portType name="Employee">
        <wsdl:operation name="getEmployeeDetails" parameterOrder="employeeIdTab">
            <wsdl:input message="tns:getEmployeeDetails"/>
            <wsdl:output message="tns:getEmployeeDetailsResponse"/>
        </wsdl:operation>
    </portType>
    <binding name="EmployeeSoapHttp" type="tns:Employee">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="getEmployeeDetails">
            <soap:operation soapAction="http://example.com/Employee.wsdl/getEmployeeDetails"/>
            <wsdl:input>
                <soap:body use="literal" namespace="http://example.com/Employee.wsdl" parts="employeeIdTab"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" namespace="http://example.com/Employee.wsdl" parts="result"/>
            </wsdl:output>
        </wsdl:operation>
    </binding>
    <service name="Employee">
        <wsdl:port name="EmployeeSoapHttpPort" binding="tns:EmployeeSoapHttp">
            <soap:address location="http://localhost:8087/ws/example/EmployeeSoapHttpPort"/>
        </wsdl:port>
    </service>
</definitions>

我有一个简单的EndPoint用于测试:

@Endpoint
public class EmployeeEndpoint {
    private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeEndpoint.class);

    @PayloadRoot(namespace = "http://example.com/Employee.wsdl", localPart = "getEmployeeDetails")
    @ResponsePayload
    public JAXBElement<EmployeeDetailRecUserArray> getEmployeeDetails(@RequestPayload JAXBElement<EmployeeIdRecUserArray> request) {
        if (request.getValue().getEmployeeIdRecUser().isEmpty())
            LOGGER.info("Request is empty.");
        else
            LOGGER.info("Request is not empty");

        return null;
    }
}

在SoapUI中加载此wsdl时,它会生成如下请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:emp="http://example.com/Employee.wsdl" xmlns:typ="http://example.com/Employee.wsdl/types/">
   <soapenv:Header/>
   <soapenv:Body>
      <emp:getEmployeeDetails>
         <employeeIdTab>
            <!--Zero or more repetitions:-->
            <typ:EmployeeIdRecUser>
               <typ:employeeId>123</typ:employeeId>
            </typ:EmployeeIdRecUser>
         </employeeIdTab>
      </emp:getEmployeeDetails>
   </soapenv:Body>
</soapenv:Envelope>

这使我在Endpoint中有一个空请求。

如果我改为发送此请求,则一切似乎都正常。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:emp="http://example.com/Employee.wsdl" xmlns:typ="http://example.com/Employee.wsdl/types/">
   <soapenv:Header/>
   <soapenv:Body>
      <emp:getEmployeeDetails>
       <!--Zero or more repetitions:-->
       <typ:EmployeeIdRecUser>
          <typ:employeeId>123</typ:employeeId>
       </typ:EmployeeIdRecUser>
      </emp:getEmployeeDetails>
   </soapenv:Body>
</soapenv:Envelope>

如何通过修改代码来解决此问题,而又不更改原始的wsdl或XML请求?

1 个答案:

答案 0 :(得分:0)

我最终改用CXF,并且能够使它工作。

@Service
@WebService(targetNamespace = "http://example.com/Employee.wsdl")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class EmployeeWebService {
    private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeWebService.class);

    @WebMethod(operationName = "getEmployeeDetails")
    @WebResult(partName = "result", targetNamespace = "http://example.com/Employee.wsdl")
    public EmployeeDetailRecUserArray getEmployeeDetails(@WebParam(partName = "employeeIdTab", targetNamespace = "http://example.com/Employee.wsdl") EmployeeIdRecUserArray employeeIdTab) {
        if (employeeIdTab.getEmployeeIdRecUser().isEmpty())
            LOGGER.info("Request is empty.");
        else
            LOGGER.info("Request is not empty");

        return null;
    }