Spring Boot's embedded tomcat is very handy, for both development and deploy.
But what if an another (3rd-party) WAR file (for example, GeoServer) should be added?
Perhaps the following is the normal procedure:
But it would be nice if the following configuration were possible.
How can it be done?
UPDATE
When the spring boot application is made of fat jar(=executable jar), the code in the answer is not enough. The revised one is as follows:
@Bean public EmbeddedServletContainerFactory servletContainerFactory() { return new TomcatEmbeddedServletContainerFactory() { @Override protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer( Tomcat tomcat) { try { Context context = tomcat.addWebapp("/foo", "/path/to/foo.war"); WebappLoader loader = new WebappLoader(Thread.currentThread().getContextClassLoader()); context.setLoader(loader); } catch (ServletException ex) { throw new IllegalStateException("Failed to add webapp", ex); } return super.getTomcatEmbeddedServletContainer(tomcat); } }; }
Since the jar files in a fat jar cannot be loaded by the system classloader, an explicit parent classloader must be specified. Otherwise, the additional WAR cannot load the library jars in the fat jar of the spring boot application that added the WAR.
1 Answer. Show activity on this post. Yes. you can only launch so many war as you have system resources such as disk and memory for.
The default Embedded Web Servers in Spring-Boot is Tomcat , but you can easily change it to others.
Perhaps the simplest way to deploy a WAR file to Tomcat is to copy the file to Tomcat's webapps directory. Copy and paste WAR files into Tomcat's webapps directory to deploy them. Tomcat monitors this webapps directory for changes, and if it finds a new file there, it will attempt to deploy it.
You can add a war file to embedded Tomcat using Tomcat.addWebapp
. As its javadoc says, it's the "equivalent to adding a web application to Tomcat's web apps directory". To use this API in Spring Boot, you need to use a custom TomcatEmbeddedServletContainerFactory
subclass:
@Bean public EmbeddedServletContainerFactory servletContainerFactory() { return new TomcatEmbeddedServletContainerFactory() { @Override protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer( Tomcat tomcat) { // Ensure that the webapps directory exists new File(tomcat.getServer().getCatalinaBase(), "webapps").mkdirs(); try { Context context = tomcat.addWebapp("/foo", "/path/to/foo.war"); // Allow the webapp to load classes from your fat jar context.setParentClassLoader(getClass().getClassLoader()); } catch (ServletException ex) { throw new IllegalStateException("Failed to add webapp", ex); } return super.getTomcatEmbeddedServletContainer(tomcat); } }; }
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