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?
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.
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.
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.
Note: In case of multiple servlet files, make sure that each pair of servlet and servlet-mapping tags must contain the same servlet-name.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With