gpt4 book ai didi

java - 如何以编程方式配置 MessageDispatcherServlet

转载 作者:行者123 更新时间:2023-11-28 22:42:21 24 4
gpt4 key购买 nike

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

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

我对如何在没有 web.xml 的情况下设置 Message Dispatcher Servlet 感到困惑。我想做的是实现 WebApplicationInitializer 接口(interface)的方法 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("/*");
}

编辑://添加了正确的方法来执行此操作,之前的答案有很多冗余。

关于java - 如何以编程方式配置 MessageDispatcherServlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26464931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com