如何以编程方式配置MessageDispatcherServlet
问题描述:
我想学习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(这是在教程示例工作)发送从来没有得到映射
答
这就是我得到了它在把工作进行到底:
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("/*");
}
编辑: //添加适当的方法来做到这一点,以前的答案有很多冗余。
答
扩展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 - 即时猜测这是错误的? – John 2014-10-20 11:56:22
缺少上下文声明,应该是:\t \t AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); ? – John 2014-10-20 15:08:57
我应该注册DispatcherServlet还是MessageDispatcherServlet?我收到错误消息:没有按照您的建议使用DispatcherServlet为http请求找到映射 – John 2014-10-20 15:41:30