如何以编程方式配置 MessageDispatcherServlet [英] How to configure MessageDispatcherServlet programmatically

查看:41
本文介绍了如何以编程方式配置 MessageDispatcherServlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to learn spring-ws and started with this tutorial: http://spring.io/guides/gs/producing-web-service/#initial.

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

What I would like to do now is to start the service on non-embedded servlet container by configuring the application programmatically.

我被困在如何在没有 web.xml 的情况下设置 Message Dispatcher Servlet.我试图做的是实现 WebApplicationInitializer 接口的方法 onStartup(ServletContext servletContext).

I am stuck on how to setup Message Dispatcher Servlet without web.xml. What i tried to is to implement WebApplicationInitializer interface's method 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(正在处理教程示例)发送的请求永远不会被映射

However, when i deploy this to tomcat, requests i send with SOAP UI( which are working on tutorial sample) are never getting mapped

推荐答案

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

This is how i got it to work in the end:

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

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

// Added proper way to do this, previous answer had a lot of redundancy.

这篇关于如何以编程方式配置 MessageDispatcherServlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆