If you package SpringBoot application as jar
, Tomcat would be included as the default embedded container.
You can't ask the embedded Tomcat to host multi web-app like what it could do as a standalone service.
So we have two choice:
Run a reverse proxy in front of your SpringBoot application. Such as Nginx
Package your SpringBoot application as a war
, and put it into some container.
For example:
- Download tomcat and start it with default configuration.
- Build a
my-app-1.0.0-SNAPSHOT.war
, rename it to myapp.war
and copy it to your tomcat's /webapps
directory.
- You can visit
http://localhost:8080/myapp
, tomcat could host all valid folders and wars in /webapps
.
PS: If you are using Spring Reactive Web
(WebFlux, Netty), the second method wouldn't work.
UPDATE
Here is what I said about ServletRegistrationBean
in comments.
// use DispatcherServlet here
private ServletRegistrationBean<? extends Servlet> createServletRegistrationBean(
ApplicationContext context, String name, String... urlMappings) {
final DispatcherServlet dispatcherServlet = new DispatcherServlet();
dispatcherServlet.setApplicationContext(context);
final ServletRegistrationBean<DispatcherServlet> servletRegistrationBean =
new ServletRegistrationBean<>(dispatcherServlet, urlMappings);
servletRegistrationBean.setName(name);
return servletRegistrationBean;
}
@Bean
public ServletRegistrationBean<? extends Servlet> oneContextPath(ApplicationContext context) {
// create applicationContext or use the auto configured one
return createServletRegistrationBean(context, "firstOne", "/*");
}
@Bean
public ServletRegistrationBean<? extends Servlet> anotherContextPath(ApplicationContext context) {
return createServletRegistrationBean(context, "secondOne", "/myapp/*");
}
As this example, we can run http GET /foo
and http GET /myapp/foo
at the same time.
Notes:
- WebFlux is NOT supported.
- Custom applicationContext if you need.