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

1

19

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?

Cremate answered 13/3, 2014 at 20:19 Comment(4)
It's not clear what you are trying to do. If you want the exact same Spring application using Servlet 3.0 why are you defining a different Servlet type (MeteorServlet vs. DispatcherServlet)? The configuration class that you load in the web.xml isn't explicitly used anywhere. Also you extend SpringBootServletInitializer but don't appear to override the crucial configure method.Ridiculous
Sorry, perhaps an example of the code you astray. I need to configure the Meteor Servlet, as described in web.xmlCremate
Sorry, not following. The web.xml has a DispatcherServlet. If you want another kind of servlet as the default servlet use the code in the answer but a different servlet class (the bean name still has to be "DispatcherServlet" though).Ridiculous
Ah, I get it. I missed the meteor servlet declaration. I'll update the answer when I get a chance.Ridiculous
R
25

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).

Ridiculous answered 14/3, 2014 at 15:16 Comment(2)
for additional url mappings use registration.addUrlMappings("/whatever/*", "/whatever2/*");Metalinguistic
I found this posting while researching how to define a filter list for a Spring Boot servlet after an error message resulted from trying to allow anonymous access to a named servlet in a Spring Boot app. Are you willing to comment? Here is the link: #36489753Calycle

© 2022 - 2024 — McMap. All rights reserved.