无法使用Spring Boot WS在WebService客户端中设置SOAP操作调用



我使用Spring Boot从Java Client致电.NET WebService时面临问题。

错误日志:

org.springframework.ws.soap.client.SoapFaultClientException: System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://staging.dayross.ca/public/shipmentservices.asmx.

我在客户端代码中添加了肥皂操作:

    @Component
    public class SOAPConnector extends WebServiceGatewaySupport {
        public Object callWebService(String url, Object request) {
            return getWebServiceTemplate().marshalSendAndReceive(url, request, new SoapActionCallback("http://staging.dayross.ca/public/shipmentservices.asmx"));
        }

    }
    //Client code:
    CreatePickup2Response result = (CreatePickup2Response) soapConnector
                    .callWebService("http://staging.dayross.ca/public/shipmentservices.asmx", pickupReq);
//Bean configuration:
@Bean
     public SOAPConnector soapConnector(Jaxb2Marshaller marshaller) {
            SOAPConnector client = new SOAPConnector();
            client.setDefaultUri("http://staging.dayross.ca/public/shipmentservices.asmx");
            client.setMarshaller(marshaller);
            client.setUnmarshaller(marshaller);
            return client;
        }

我很确定我在这里缺少一些基本配置,但无法弄清楚。有人可以帮我解决这个问题吗?

完成跟踪:

2019-05-11 22:21:06.685 DEBUG 21052 --- [  restartedMain] o.s.ws.client.core.WebServiceTemplate    : Opening [org.springframework.ws.transport.http.HttpUrlConnection@4c12c5cb] to [http://staging.dayross.ca/public/shipmentservices.asmx]
2019-05-11 22:21:06.719 TRACE 21052 --- [  restartedMain] o.s.ws.client.MessageTracing.sent        : Sent request [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:CreatePickup2 xmlns:ns2="http://dayrossgroup.com/web/public/webservices/shipmentServices" xmlns:ns3="http://www.dayrossgroup.com/web/common/webServices/OnlineShipping"><ns2:division>GeneralFreight</ns2:division><ns2:emailAddress>staging@dr.com</ns2:emailAddress><ns2:password>test</ns2:password><ns2:shipment><ns2:ShipperAddress><ns2:Address1>8345 WHITE OAK AVENUE</ns2:Address1><ns2:City>Saint-Augustin-De-Desmaures</ns2:City><ns2:Country>CA</ns2:Country><ns2:Name>SHIPPER CONTACT NAME</ns2:Name><ns2:PostalCode>G3A0G2</ns2:PostalCode><ns2:Province>QC</ns2:Province><ns2:CompanyName>ADVANTAGE</ns2:CompanyName><ns2:EmailAddress>SHIPPEREMAIL@EMAIL.COM</ns2:EmailAddress><ns2:PhoneNumber>9092044990</ns2:PhoneNumber></ns2:ShipperAddress><ns2:ConsigneeAddress><ns2:Address1>5622 BURLEIGH CRESCENT SE</ns2:Address1><ns2:City>CALGARY</ns2:City><ns2:Country>CA</ns2:Country><ns2:Name>CONSIGNEE CONTACT NAME</ns2:Name><ns2:PostalCode>T2H1Z8</ns2:PostalCode><ns2:Province>AB</ns2:Province><ns2:CompanyName>LOWRY</ns2:CompanyName><ns2:PhoneNumber>8964567412</ns2:PhoneNumber></ns2:ConsigneeAddress><ns2:BillToAccount>56896</ns2:BillToAccount><ns2:Items><ns2:ShipmentItem><ns2:Description>SKID OF TAPE</ns2:Description><ns2:Height>24</ns2:Height><ns2:Length>30</ns2:Length><ns2:LengthUnit>Inches</ns2:LengthUnit><ns2:Pieces>2</ns2:Pieces><ns2:Weight>50</ns2:Weight><ns2:WeightUnit>Pounds</ns2:WeightUnit><ns2:Width>30</ns2:Width></ns2:ShipmentItem></ns2:Items><ns2:ServiceLevel>GL</ns2:ServiceLevel><ns2:ShipmentType>Regular</ns2:ShipmentType><ns2:ReadyTime>2019-05-19 15:19:27</ns2:ReadyTime><ns2:ClosingTime>2019-05-19 17:19:27</ns2:ClosingTime><ns2:ShipmentStatus><ns2:Id xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/><ns2:RowVersion>0</ns2:RowVersion><ns2:InternalStatus>false</ns2:InternalStatus><ns2:OrderEntryState>ReadyForPickup</ns2:OrderEntryState></ns2:ShipmentStatus><ns2:MeasurementSystem>Imperial</ns2:MeasurementSystem><ns2:ExpiryDate>2029-01-07T22:21:06.659-06:00</ns2:ExpiryDate><ns2:Division>GeneralFreight</ns2:Division><ns2:ReferenceNumbers><ns2:string>RA89653</ns2:string></ns2:ReferenceNumbers></ns2:shipment><ns2:language>EN</ns2:language></ns2:CreatePickup2></SOAP-ENV:Body></SOAP-ENV:Envelope>]
2019-05-11 22:21:06.970 TRACE 21052 --- [  restartedMain] o.s.ws.client.MessageTracing.received    : Received response [<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><soap:Fault><faultcode>soap:Client</faultcode><faultstring>System.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction: http://staging.dayross.ca/public/shipmentservices.asmx.
   at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()
   at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
   at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
   at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)</faultstring><detail/></soap:Fault></soap:Body></soap:Envelope>] for request [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:CreatePickup2 xmlns:ns2="http://dayrossgroup.com/web/public/webservices/shipmentServices" xmlns:ns3="http://www.dayrossgroup.com/web/common/webServices/OnlineShipping"><ns2:division>GeneralFreight</ns2:division><ns2:emailAddress>staging@dr.com</ns2:emailAddress><ns2:password>test</ns2:password><ns2:shipment><ns2:ShipperAddress><ns2:Address1>8345 WHITE OAK AVENUE</ns2:Address1><ns2:City>Saint-Augustin-De-Desmaures</ns2:City><ns2:Country>CA</ns2:Country><ns2:Name>SHIPPER CONTACT NAME</ns2:Name><ns2:PostalCode>G3A0G2</ns2:PostalCode><ns2:Province>QC</ns2:Province><ns2:CompanyName>ADVANTAGE</ns2:CompanyName><ns2:EmailAddress>SHIPPEREMAIL@EMAIL.COM</ns2:EmailAddress><ns2:PhoneNumber>9092044990</ns2:PhoneNumber></ns2:ShipperAddress><ns2:ConsigneeAddress><ns2:Address1>5622 BURLEIGH CRESCENT SE</ns2:Address1><ns2:City>CALGARY</ns2:City><ns2:Country>CA</ns2:Country><ns2:Name>CONSIGNEE CONTACT NAME</ns2:Name><ns2:PostalCode>T2H1Z8</ns2:PostalCode><ns2:Province>AB</ns2:Province><ns2:CompanyName>LOWRY</ns2:CompanyName><ns2:PhoneNumber>8964567412</ns2:PhoneNumber></ns2:ConsigneeAddress><ns2:BillToAccount>56896</ns2:BillToAccount><ns2:Items><ns2:ShipmentItem><ns2:Description>SKID OF TAPE</ns2:Description><ns2:Height>24</ns2:Height><ns2:Length>30</ns2:Length><ns2:LengthUnit>Inches</ns2:LengthUnit><ns2:Pieces>2</ns2:Pieces><ns2:Weight>50</ns2:Weight><ns2:WeightUnit>Pounds</ns2:WeightUnit><ns2:Width>30</ns2:Width></ns2:ShipmentItem></ns2:Items><ns2:ServiceLevel>GL</ns2:ServiceLevel><ns2:ShipmentType>Regular</ns2:ShipmentType><ns2:ReadyTime>2019-05-19 15:19:27</ns2:ReadyTime><ns2:ClosingTime>2019-05-19 17:19:27</ns2:ClosingTime><ns2:ShipmentStatus><ns2:Id xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/><ns2:RowVersion>0</ns2:RowVersion><ns2:InternalStatus>false</ns2:InternalStatus><ns2:OrderEntryState>ReadyForPickup</ns2:OrderEntryState></ns2:ShipmentStatus><ns2:MeasurementSystem>Imperial</ns2:MeasurementSystem><ns2:ExpiryDate>2029-01-07T22:21:06.659-06:00</ns2:ExpiryDate><ns2:Division>GeneralFreight</ns2:Division><ns2:ReferenceNumbers><ns2:string>RA89653</ns2:string></ns2:ReferenceNumbers></ns2:shipment><ns2:language>EN</ns2:language></ns2:CreatePickup2></SOAP-ENV:Body></SOAP-ENV:Envelope>]
2019-05-11 22:21:06.971 DEBUG 21052 --- [  restartedMain] o.s.ws.client.core.WebServiceTemplate    : Received Fault message for request [SaajSoapMessage {http://dayrossgroup.com/web/public/webservices/shipmentServices}CreatePickup2]

您的问题是您要执行操作的服务器无法识别SOAPAction标头,因此无法将请求路由到适当的端点。如果您查看要连接到的服务的WSDL,则可以执行许多不同的操作。

对于您的特定示例,CreatePickup2,该操作被指定为

<wsdl:operation name="CreatePickup2">
    <soap:operation soapAction="http://dayrossgroup.com/web/public/webservices/shipmentServices/CreatePickup2" style="document"/>
    <wsdl:input>
        <soap:body use="literal"/>
    </wsdl:input>
    <wsdl:output>
        <soap:body use="literal"/>
    </wsdl:output>
</wsdl:operation>

由于您已经在bean中指定了defaultUri,因此您不需要在getWebServiceTemplate().marshalSendAndReceive(...)方法中指定URL。

将为您提供像这样的组件

@Component
public class SOAPConnector extends WebServiceGatewaySupport {
    public Object callWebService(Object request, String soapAction) {
        return getWebServiceTemplate().marshalSendAndReceive(request, new SoapActionCallback(soapAction));
    }
}

和一个客户代码类似于此

CreatePickup2Response result = (CreatePickup2Response) soapConnector
        .callWebService(pickupReq, "http://dayrossgroup.com/web/public/webservices/shipmentServices/CreatePickup2");

如果您将使用不止一个操作,则需要使我的建议解决方案更通用。

最新更新