我正在尝试学习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(正在处理教程示例)发送的请求永远不会被映射
答案 0 :(得分: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("/*");
}
编辑 //添加了正确的方法,以前的答案有很多冗余。
答案 1 :(得分:0)
扩展AbstractAnnotationConfigDispatcherServletInitializer并实现3个剩余方法 +在onStartup
中添加自己的servletimport 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("/*");
}
}