Spring-Integration-DSL & Soap Service Soap header is not getting invoked

572 views Asked by At

I am trying to call a soap service with spring integration DSL, have some custom header that needs to be added.

Constructed marshellingoutboundgateway. Trying to override DefaultSoapHeaderMapper but none of the overridden methods are getting called.

Trying to construct some thing like this.

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

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

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

There is a sample in : https://github.com/spring-projects/spring-integration/blob/master/src/reference/asciidoc/ws.adoc

looks like only available in 5.0

posted detail code.

Any insights will be helpful.

@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;
    }
}
1

There are 1 answers

1
kiran reddy On BEST ANSWER

UPDATE: Modified the code

@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);
            }
        };

    }

and set the header using the method call:

gateway.setHeaderMapper(headerMapper11());

Now the overridden method is getting called and having the header as well.

Code is working as expected now.