Search code examples
javaspringspring-ws

How to register MessageDispatcherServlet in a classic (non Boot) Spring application


I am currently in the process of developing a web application that is accessible via SOAP using Spring 5.1.3 with Spring-WS and have no clue how to register an additional servlet (in this case, MessageDispatcherServlet for Spring-WS) using Java config. I should note that this is a non Boot application.

I consulted the official Spring docs for help, however this guide is geared towards Spring Boot (which uses ServletRegistrationBean, which is exclusive to Spring Boot). According to the guide, the MessageDispatcherServlet is registered like this:

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {

    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean(servlet, "/ws/*");
    }
}

This looks nice and straightforward apart from the fact that ServletRegistrationBean resides in org.springframework.boot.web.servlet => Spring Boot => not available to me. How do I register my MessageDispatherServlet in a "non Boot" standard Spring application? Thanks a lot for any hints or advice.


Solution

  • Thanks for any pointers everyone. I managed to register the MessageDispatcherServlet via WebApplicationInitializer:

    public class AppInitializer implements WebApplicationInitializer {
    
        @Override
        public void onStartup(ServletContext container) throws ServletException {
            AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
            context.register(WebConfig.class);
            container.addListener(new ContextLoaderListener(context));
    
            // Message Dispatcher Servlet (SOAP)
            MessageDispatcherServlet messageDispatcherServlet = new MessageDispatcherServlet();
            messageDispatcherServlet.setApplicationContext(context);
            messageDispatcherServlet.setTransformWsdlLocations(true);
            ServletRegistration.Dynamic messageDispatcher = container.addServlet("messageDispatcher", messageDispatcherServlet);
            messageDispatcher.setLoadOnStartup(1);
            messageDispatcher.addMapping("/ws/*");
        }
    }
    

    Pretty straightforward once you know how to do it :D