2

I'm trying to wrap my head around spring integration and would preferably like to stay away from XML-based configuration.

What I would like to do is the following:

  1. Accept a SOAP request on a certain endpoint
  2. Forward this request to a downstream SOAP service, without any modification (for now I just want the basics to work)
  3. Return the result to the client

This seems like it should be pretty simple, but I don't have the faintest idea of where to start. Is this even possible with SI? As I understand it, and please correct me if I'm wrong, SI is mainly used for async data flows.

I did check out the integration sample repository, which includes example inbound WS requests, but these are all configured in XML, which, as I said, I'd preferably like to stay far away from.

Any pointers would be much appreciated; I've been reading through documentation for the past two days and I'm none the wiser!

4 Answers 4

7

Here is an example using the SimpleWebServiceInboundGateway. In this example we also set the "ExtractPayload" to false so that it sends the RAW soap message. But agree with above, possibly the HTTPInboundRequest is better for your use case. I also didn't find many examples using DSL for the SoapInboundGateway so wanted to share and hope it helps someone else.

@Configuration
@EnableIntegration
public class SoapGatewayConfiguration {

    /**
     * URL mappings used by WS endpoints
     */
    public static final String[] WS_URL_MAPPINGS = {"/services/*", "*.wsdl", "*.xsd"};
    public static final String GATEWAY_INBOUND_CHANNEL_NAME  = "wsGatewayInboundChannel";
    public static final String GATEWAY_OUTBOUND_CHANNEL_NAME = "wsGatewayOutboundChannel";


    /**
     * Register the servlet mapper, note that it uses MessageDispatcher
     */
    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        servlet.setTransformSchemaLocations(true);
        servlet.setPublishEvents(true);
        ServletRegistrationBean servletDef = new ServletRegistrationBean(servlet, WS_URL_MAPPINGS);
        servletDef.setLoadOnStartup(1);
        return servletDef;
    }

    /**
     * Create a new Direct channels to handle the messages
     */
    @Bean
    public MessageChannel wsGatewayInboundChannel() {
        return MessageChannels.direct(GATEWAY_INBOUND_CHANNEL_NAME).get();
    }
    @Bean
    public MessageChannel wsGatewayOutboundChannel() {
        return MessageChannels.direct(GATEWAY_OUTBOUND_CHANNEL_NAME).get();
    }

    /**
     * Startup the WebServiceInboundGateway Endpoint, this will handle the incoming SOAP requests
     *  and place them onto the request channel
     */
    @Bean
    public SimpleWebServiceInboundGateway webServiceInboundGateway(
            @Value("${spring.ws.request.timeout:1000}") long requestTimeout,
            @Value("${spring.ws.reply.timeout:1000}") long replyTimeout,
            @Value("${spring.ws.should.track:true}") boolean shouldTrack
    ) {
        SimpleWebServiceInboundGateway wsg = new SimpleWebServiceInboundGateway();
        wsg.setRequestChannel(wsGatewayInboundChannel());
        wsg.setReplyChannel(wsGatewayOutboundChannel());
        wsg.setExtractPayload(false);  // Send the full RAW SOAPMessage and not just payload
        wsg.setLoggingEnabled(true);
        wsg.setShouldTrack(shouldTrack);
        wsg.setReplyTimeout(replyTimeout); // Do not believe this prop supported currently
        wsg.setRequestTimeout(requestTimeout); // Do not believe this prop is supported currently
        wsg.setCountsEnabled(true);
        return wsg;
    }

    /**
     * You must enable debug logging on org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor
     *  to see the logs from this interceptor
     */
    @Bean
    public EndpointInterceptor soapMessageLoggingInterceptor() {
        SoapEnvelopeLoggingInterceptor li = new SoapEnvelopeLoggingInterceptor();
        li.setLogRequest(true);
        li.setLogResponse(true);
        li.setLogFault(true);
        return li;
    }


    /**
     * Validate the incoming web service against the schema
     */
    @Bean
    public EndpointInterceptor payloadValidatingInterceptor(XsdSchema xsdSchema
            , @Value("${spring.ws.soap.validate.request:true}") boolean soapValidateRequest
            , @Value("${spring.ws.soap.validate.reply:true}") boolean soapValidateResponse
            , @Value("${spring.ws.soap.validate.addErrorDetail:true}") boolean soapAddValidationErrorDetail

    ) {
        PayloadValidatingInterceptor interceptor = new PayloadValidatingInterceptor();
        interceptor.setXsdSchema(xsdSchema);
        interceptor.setValidateRequest(soapValidateRequest);
        interceptor.setValidateResponse(soapValidateResponse);
        interceptor.setAddValidationErrorDetail(soapAddValidationErrorDetail);
        return interceptor;
    }

    /**
     * Map the allowable service Uri's.
     */
    @Bean
    public EndpointMapping uriEndpointMapping(
             PayloadValidatingInterceptor payloadValidatingInterceptor
            , SimpleWebServiceInboundGateway webServiceInboundGateway
            , SoapEnvelopeLoggingInterceptor loggingInterceptor) {
        UriEndpointMapping mapping = new UriEndpointMapping();
        mapping.setUsePath(true);
        mapping.setDefaultEndpoint(webServiceInboundGateway);
        mapping.setInterceptors(new EndpointInterceptor[]{loggingInterceptor, payloadValidatingInterceptor});
        return mapping;
    }

    /**
     * Expose the wsdl at http://localhost:8080/services/myService.wsdl
     **/    
    @Bean
    public Wsdl11Definition myService() {
        SimpleWsdl11Definition wsdl11Definition = new SimpleWsdl11Definition();
        wsdl11Definition.setWsdl(new ClassPathResource("META-INF/myService.wsdl"));
        return wsdl11Definition;
    }
    
    /**
     * Expose the xsd at http://localhost:8080/services/mySchema.xsd
     **/
    @Bean
    public XsdSchema mySchema() {
        return new SimpleXsdSchema(new ClassPathResource("META-INF/mySchema.xsd"));
    }    

    @Bean
    public IntegrationFlow itemLookupFlow() {
        return IntegrationFlows.from("wsGatewayInboundChannel")
                .log(LoggingHandler.Level.INFO)
                .handle(myBeanName, "execute")
                .log(LoggingHandler.Level.TRACE, "afterExecute")
                .get();
    }

}
3
  • Hi, I have a similar need to that of the OP: I need to expose a SOAP endpoint with a certain configuration (wsdl) and forward the same message, unaltered, to another endpoint with a slightly different configuration. That's because I can't change neither the source or the destination. I'm trying to use your code but I'm having a few problems. First, I'm totally new to both SOAP and SI. I'm trying to mix your code with SI's "ws-inbound-gateway" sample with little luck yet. Second, there's an undefined variable named "myBeanName" in your code, what should go there? Thanks for any help. Jul 18, 2018 at 20:26
  • any spring bean which exposes a method named "execute" (see second parameter to .handle)
    – abe
    Jul 19, 2018 at 21:10
  • Thanks, that makes sense. Jul 20, 2018 at 16:31
4

If your application is just a proxy over other SOAP service, you should consider to use just plain HTTP Inbound Gateway and HTTP Outbound Gateway.

You receive an XML from client and send it into the downstream service. Receive from there an XML again and just push it back to the response for the client.

For this purpose I can suggest HTTP proxy solution via Java DSL:

    @Bean
    public IntegrationFlow httpProxyFlow() {
        return IntegrationFlows
                .from(Http.inboundGateway("/service"))
                .handle(Http.outboundGateway("/service/internal")
                                .expectedResponseType(String.class))
                .get();
    }

The problem with the SimpleWebServiceInboundGateway and SimpleWebServiceOutboundGateway pair that they extract a request and parse a respose to (un)wrap to/from the SOAP envelop. This looks like an overhead for your plain proxy use-case.

0

I got it working thanks to Artem's answer, with a small tweak. Not sure as to why the channels are required, but at least it's now working.

package com.example.integration;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.dsl.http.Http;
import org.springframework.integration.http.config.EnableIntegrationGraphController;
import org.springframework.messaging.MessageChannel;

@SpringBootApplication
@EnableIntegration
@EnableIntegrationGraphController(allowedOrigins = "*")
public class IntegrationApplication {

    public static void main(String[] args) {
        SpringApplication.run(IntegrationApplication.class, args);
    }

    @Bean
    public DirectChannel directChannel() {
        return new DirectChannel();
    }

    @Bean
    public IntegrationFlow httpProxyFlow(MessageChannel directChannel) {
        return IntegrationFlows
                .from(Http.inboundGateway("/thing").requestChannel(directChannel).replyChannel(directChannel))
                .enrichHeaders(h -> h.header("Content-Type", "application/soap+xml; charset=utf-8"))
                .handle(Http.outboundGateway("http://www.webservicex.net/geoipservice.asmx").expectedResponseType(String.class))
                .channel(directChannel)
                .get();
    }
}
1
  • @ArtemBilan so this is weird. Last night it was giving me an error about "channel does not have subscribers" without the channels. I quickly re-tried this without the channels, now it's working. No idea. Spring is funny that way I guess. Dec 1, 2017 at 5:37
0

Using Spring Integration Gateway is a best option to route HTTP message.

Incase if you want to go with Spring MVC and don't need to parse the SOAP Body.

Define the controller to receive the body as a String and pass it down the down systems and send back the response entity which you receive from down system.

For Ex :

public ResponseEntity<String> serviceName (@RequestBody String soapBody,
@RequestHeader HttpHeaders headers) {
HittpEntity<String> entity - new HttEntity<>(input, headers) ;
ResponseEntity<String> responseEntity   = restlemplate…;
return responseEntity;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.