如何使用 spring 集成 java dsl 和操作名称调用 soap webservice

how to call soap webservice using spring integration java dsl with operation name

我正在尝试使用 spring 集成的 MarshallingWebServiceOutboundGateway 调用 soap 网络服务。以下是我的流程,非常简单:

@Bean
public IntegrationFlow asghar() throws Exception {
    Map<String, Object> action = new HashMap<>();
    action.put("ws_soapAction", "getCardTypes");

    return IntegrationFlows.from("inputChannel")
            .enrichHeaders(action)
            .handle(asgharWebserviceGateway()).get();
} 

通过 "inputChannel" 传递的消息负载中的对象属于 CardGroup 类型。然后我按如下方式创建网关:

    @Bean
    public MarshallingWebServiceOutboundGateway asgharWebserviceGateway() throws Exception {
        SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory(
                MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL));
        messageFactory.afterPropertiesSet();

        WebServiceTemplate webServiceTemplate = new WebServiceTemplate(messageFactory);

        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();

        marshaller.setContextPath("my.package.to.webservice.entities");
        marshaller.setCheckForXmlRootElement(false);
        marshaller.afterPropertiesSet();

        webServiceTemplate.setMarshaller(marshaller);
        webServiceTemplate.afterPropertiesSet();
        MarshallingWebServiceOutboundGateway asghar = new MarshallingWebServiceOutboundGateway("http://uri.to.webservice/MobileService", webServiceTemplate);

        asghar.setReplyChannel(replyChannel());
        return asghar;
    }

这是cxf从wsdl生成的服务接口的一部分

@WebMethod
@WebResult(name = "return", targetNamespace = "http://ws.gateway.manager.br.com/", partName = "return")
public CardTypesResponse getCardTypes(

        @WebParam(partName = "cardGroup", name = "cardGroup")
                CardGroup cardGroup
);

这是同一部分的 wsdl:

  <wsdl:message name="getCardTypes">
    <wsdl:part name="cardGroup" type="tns:cardGroup">
    </wsdl:part>
  </wsdl:message>

  <wsdl:message name="getCardTypesResponse">
    <wsdl:part name="return" type="tns:cardTypesResponse">
    </wsdl:part>
  </wsdl:message>

  <wsdl:operation name="getCardTypes">
    <wsdl:input message="tns:getCardTypes" name="getCardTypes">
    </wsdl:input>
    <wsdl:output message="tns:getCardTypesResponse" name="getCardTypesResponse">
    </wsdl:output>
  </wsdl:operation>
  <wsdl:operation name="getCardTypes">
    <soap:operation soapAction="" style="rpc"/>
      <wsdl:input name="getCardTypes">
        <soap:body namespace="http://ws.gateway.manager.br.com/" use="literal"/>
      </wsdl:input>
      <wsdl:output name="getCardTypesResponse">
        <soap:body namespace="http://ws.gateway.manager.br.com/" use="literal"/>
      </wsdl:output>
  </wsdl:operation>

如您所见,wsdl 中没有 soapAction 并且上述代码生成此 soap 消息:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <tns:cardGroup xmlns:tns="http://ws.gateway.manager.br.com/">
            <code>9865421</code>
            <title>654965587</title>
        </tns:cardGroup>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

现在我想知道如何使用操作名称 (getCardTypes) 以及我应该在哪里设置它以便正确创建 soap 消息,它必须是这样的:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">

    <SOAP-ENV:Header>

    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <tns:getCardTypes xmlns:tns="http://ws.gateway.manager.br.com/">
             <tns:cardGroup xmlns:tns="http://ws.gateway.manager.br.com/">
                <code>9865421</code>
                <title>654965587</title>
            </tns:cardGroup>
        </tns:getCardTypes>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
<wsdl:message name="getCardTypes">
    <wsdl:part name="cardGroup" type="tns:cardGroup">
    </wsdl:part>
</wsdl:message>

因此,您的 CardGroup 对象必须包装到 GetCardTypes 对象中。 Spring WS 不是 CXF,因此您需要习惯它的 Contract First 方法。