【问题标题】:How to configure MessageDispatcherServlet programmatically如何以编程方式配置 MessageDispatcherServlet
【发布时间】:2014-12-15 09:25:56
【问题描述】:

我正在尝试学习 spring-ws 并从本教程开始: http://spring.io/guides/gs/producing-web-service/#initial.

我现在想做的是通过以编程方式配置应用程序,在非嵌入式 servlet 容器上启动服务。

我不知道如何在没有 web.xml 的情况下设置 Message Dispatcher Servlet。我试图做的是 实现WebApplicationInitializer接口的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("/*");


    }

}

但是,当我将它部署到 tomcat 时,我使用 SOAP UI(正在处理教程示例)发送的请求永远不会被映射

【问题讨论】:

    标签: java spring tomcat servlets


    【解决方案1】:

    扩展 AbstractAnnotationConfigDispatcherServletInitializer 并实现剩下的 3 个方法 + 在 onStartup 中添加你自己的 servlet

    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("/*");
        }
    
    }
    

    【讨论】:

    • 我试过这个没有成功。对于 RootContextConfiguration,我提供了 WebServiceConfig 类 - 就像教程中的一个,但没有 ServletRegistrationBean。对于 servletConfigClasses,我设置了 CountryEndpoint - 我猜这是错误的?
    • 缺少上下文声明,应该是:AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); ?
    • 我应该注册 DispatcherServlet 还是 MessageDispatcherServlet ?我收到错误:没有找到您建议的使用 DispatcherServlet 的 http 请求的映射
    【解决方案2】:

    这就是我最终让它工作的方式:

    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("/*");
        }
    

    编辑://添加了正确的方法来做到这一点,以前的答案有很多冗余。

    【讨论】:

      猜你喜欢
      • 2016-11-20
      • 2011-02-17
      • 2012-10-09
      • 1970-01-01
      • 2019-11-16
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      相关资源
      最近更新 更多