How to configure MessageDispatcherServlet programmatically

不问归期 提交于 2021-02-08 11:51:07

问题


I am trying to learn spring-ws and started with this tutorial: http://spring.io/guides/gs/producing-web-service/#initial.

What I would like to do now is to start the service on non-embedded servlet container by configuring the application programmatically.

I am stuck on how to setup Message Dispatcher Servlet without web.xml. What i tried to is to implement WebApplicationInitializer interface's method onStartup(ServletContext servletContext).

public class ServerInitializer  implements WebApplicationInitializer{

    public void onStartup(ServletContext servletContext) throws ServletException {

        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation(WebServiceConfig.class.getName());
        servletContext.addListener(new ContextLoaderListener(context));

        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(context);
        servlet.setTransformWsdlLocations(true);


        // Create dispatcher for named context
        ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("DispatcherServlet", servlet);

        // Load on startup
        dispatcherServlet.setLoadOnStartup(1);

        // Add URL mapping for dispatcher
        dispatcherServlet.addMapping("/*");


    }

}

However, when i deploy this to tomcat, requests i send with SOAP UI( which are working on tutorial sample) are never getting mapped


回答1:


This is how i got it to work in the end:

public class WebServiceInitializer implements WebApplicationInitializer {

    private static final String ACTIVE_PROFILE = "production";

    /**
     * Registers and loads on startup MessageDispatcherServlet for the SOAP messages
     */
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();

        // @EnableWs, @Configuration, @ComponentScan
        context.setConfigLocation(WebServiceBeans.class.getName());
        context.getEnvironment().setActiveProfiles(ACTIVE_PROFILE);

        // use MessageDispatcherServlet instead of standard DispatcherServlet for SOAP messages
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setContextClass(AnnotationConfigWebApplicationContext.class);
        servlet.setApplicationContext(context);
        servlet.setTransformWsdlLocations(true);

        // register MessageDispatcherServlet as Web Service entry point
        final ServletRegistration.Dynamic dispatcher = servletContext.addServlet("MessageDispatcherServlet",
                servlet);

        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/*");
    }

Edit:// Added proper way to do this, previous answer had a lot of redundancy.




回答2:


Extend AbstractAnnotationConfigDispatcherServletInitializer and implement 3 remaining methods + add your own servlet inside onStartup

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/*" };
    }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { RootContextConfiguration.class, SecurityConfiguration.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfiguration.class };
    }

    @Override
    public void onStartup(final ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        final ServletRegistration.Dynamic dispatcher = servletContext.addServlet("someotherServlet",
                new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/*");
    }

}


来源:https://stackoverflow.com/questions/26464931/how-to-configure-messagedispatcherservlet-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!