2014-10-20 142 views
0

我想學習spring-ws並從本教程開始: http://spring.io/guides/gs/producing-web-service/#initial如何以編程方式配置MessageDispatcherServlet

我現在要做的是通過編程配置應用程序來啓動非嵌入式servlet容器上的服務。

我被困在如何設置沒有web.xml的消息分派器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(這是在教程示例工作)發送從來沒有得到映射

回答

1

這就是我得到了它在把工作進行到底:

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

編輯: //添加適當的方法來做到這一點,以前的答案有很多冗餘。

0

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

} 
+0

我試過這個沒有成功。對於RootContextConfiguration,我提供了WebServiceConfig類 - 就像教程中的一樣,但沒有ServletRegistrationBean。對於servletConfigClasses,我設置了CountryEndpoint - 即時猜測這是錯誤的? – John 2014-10-20 11:56:22

+0

缺少上下文聲明,應該是:\t \t AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); ? – John 2014-10-20 15:08:57

+0

我應該註冊DispatcherServlet還是MessageDispatcherServlet?我收到錯誤消息:沒有按照您的建議使用DispatcherServlet爲http請求找到映射 – John 2014-10-20 15:41:30