1

我正在尝试使用 spring 集成 DSL 调用肥皂服务,需要添加一些自定义标头。

构建了marshellingoutboundgateway。试图覆盖 DefaultSoapHeaderMapper 但没有调用任何被覆盖的方法。

试图构建这样的东西。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header>
        <ObjectType34 >

        </ObjectType34>
    </soapenv:Header>
    <soapenv:Body>
      <ObjectType12 >

        </ObjectType12>
    </soapenv:Body>
</soapenv:Envelope>

有一个示例:https ://github.com/spring-projects/spring-integration/blob/master/src/reference/asciidoc/ws.adoc

看起来只在 5.0 中可用

贴出详细代码。

任何见解都会有所帮助。

@Configuration
@SpringBootApplication
@IntegrationComponentScan
@EnableIntegration
public class Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
        Info Info = ctx.getBean(Info.class);

        //Constructing request Payload
        ObjectType12 getInfoType = new ObjectFactory().ObjectType12();
        JAXBElement<GetInfoType> getInfoTypeJAXBElement = new ObjectFactory().createGetInfo(getInfoType);
        JAXBElement<GetInfoResponseType> getInfoResponseType = Info.getInfo(getInfoTypeJAXBElement);
        System.out.println(getInfoResponseType.getName());
        ctx.close();
    }

    @MessagingGateway
    public interface Info {

        @Gateway(requestChannel = "convert.input")
        JAXBElement<GetInfoResponseType> getInfo(JAXBElement<GetInfoType> InfoType);

    }

    @Bean
    public IntegrationFlow convert() {

        StringResult result = new StringResult();

        return flow -> flow
                .wireTap(f -> f.<JAXBElement, String>transform(ele -> {
                    jaxb2Marshaller().marshal(ele, result);
                    return result.toString();
                }).log())
                .handle(endpoint());
    }

    @Bean
    public Jaxb2Marshaller jaxb2Marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setPackagesToScan("org.abc", "com.abc");
        return marshaller;
    }

    @Bean
    public MarshallingWebServiceOutboundGateway endpoint() {
        MarshallingWebServiceOutboundGateway gateway = new MarshallingWebServiceOutboundGateway("https://example.com/v1", jaxb2Marshaller(), jaxb2Marshaller());

        gateway.setHeaderMapper(new DefaultSoapHeaderMapper() {

            @Override
            protected void populateUserDefinedHeader(String headerName, Object headerValue, SoapMessage target) {
                super.populateUserDefinedHeader("Content-Type", "application/soap+xml", target);
            }

            @Override
            protected void populateStandardHeaders(Map<String, Object> headers, SoapMessage target) {
                headers.put(WebServiceHeaders.SOAP_ACTION,
                        "http://www.example.com/SOAUI/ServiceHeader/V4");
                super.populateStandardHeaders(headers, target);
            }

            @Override
            public void fromHeadersToRequest(MessageHeaders headers, SoapMessage target) {
                SaajSoapMessage targetMessage = (SaajSoapMessage) target;
                SoapHeader header = targetMessage.getEnvelope().getHeader();
                //Constructing SOAP Header

                JAXBElement<ObjectType34> trackingHdrTypeJAXBElement = ObjectFactory().createHdr(ObjectType34);
                jaxb2Marshaller().marshal(trackingHdrTypeJAXBElement, header.getResult());
                System.out.println(header.getResult());
            }

            @Override
            public void setRequestHeaderNames(String... requestHeaderNames) {
                super.setRequestHeaderNames("*");
            }
        });
        gateway.setMessageFactory(new SaajSoapMessageFactory() {

            @Override
            public void setSoapVersion(SoapVersion version) {
                super.setSoapVersion(SoapVersion.SOAP_11);
            }
        });
        return gateway;
    }
}
4

1 回答 1

1

更新:修改了代码

@Bean
    public DefaultSoapHeaderMapper headerMapper11() {

        return new DefaultSoapHeaderMapper() {
            @Override
            public void fromHeadersToRequest(MessageHeaders headers, SoapMessage target) {
                SaajSoapMessage targetMessage = (SaajSoapMessage) target;
                SoapHeader header = targetMessage.getEnvelope().getHeader();
                //Constructing SOAP Header
                JAXBElement<ObjectType34> trackingHdrTypeJAXBElement = ObjectFactory().createHdr(ObjectType34);
                jaxb2Marshaller().marshal(trackingHdrTypeJAXBElement, header.getResult());
                super.fromHeadersToRequest(headers, target);
            }
        };

    }

并使用方法调用设置标题:

gateway.setHeaderMapper(headerMapper11());

现在被覆盖的方法被调用并且也有标题。

代码现在按预期工作。

于 2017-09-05T20:23:19.657 回答