Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure spring-boot servlet like in web.xml?

I have a simple servlet configuration in web.xml:

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.atmosphere.cpr.MeteorServlet</servlet-class>
    <init-param>
        <param-name>org.atmosphere.servlet</param-name>
        <param-value>org.springframework.web.servlet.DispatcherServlet</param-value>
    </init-param>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>net.org.selector.animals.config.ComponentConfiguration</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

How can I rewrite it for SpringBootServletInitializer?

like image 879
Selector Avatar asked Mar 13 '14 20:03

Selector


People also ask

How can we configure a servlet using web xml file?

To map a URL to a servlet, you declare the servlet with the <servlet> element, then define a mapping from a URL path to a servlet declaration with the <servlet-mapping> element.

Can I use web xml in Spring Boot?

Spring MVC web applications use the web. xml file as a deployment descriptor file. Also, it defines mappings between URL paths and the servlets in the web. xml file.

Does Spring Boot support xml configuration?

Before Spring 3.0, XML was the only way to define and configure beans. Spring 3.0 introduced JavaConfig, allowing us to configure beans using Java classes. However, XML configuration files are still used today. In this tutorial, we'll discuss how to integrate XML configurations into Spring Boot.

Can we configure multiple servlets in web xml file?

Note: In case of multiple servlet files, make sure that each pair of servlet and servlet-mapping tags must contain the same servlet-name.


1 Answers

If I take your question at face value (you want a SpringBootServletInitializer that duplicates your existing app) I guess it would look something like this:

@Configuration
public class Restbucks extends SpringBootServletInitializer {

    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Restbucks.class, ComponentConfiguration.class);
    }

    @Bean
    public MeteorServlet dispatcherServlet() {
        return new MeteorServlet();
    }

    @Bean
    public ServletRegistrationBean dispatcherServletRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet());
        Map<String,String> params = new HashMap<String,String>();
        params.put("org.atmosphere.servlet","org.springframework.web.servlet.DispatcherServlet");
        params.put("contextClass","org.springframework.web.context.support.AnnotationConfigWebApplicationContext");
        params.put("contextConfigLocation","net.org.selector.animals.config.ComponentConfiguration");
        registration.setInitParameters(params);
        return registration;
    }

}

See docs on converting an existing app for more detail.

But, rather than using Atmosphere, you are probably better off these days using the native Websocket support in Tomcat and Spring (see the websocket sample and guide for examples).

like image 187
Dave Syer Avatar answered Oct 02 '22 20:10

Dave Syer