Spring Boot: How to add another WAR files to the embedded tomcat?

后端 未结 3 1809
逝去的感伤
逝去的感伤 2020-11-28 08:28

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?<

3条回答
  •  盖世英雄少女心
    2020-11-28 08:48

    The accepted answer covers Spring Boot 1.x. The class mentioned is no longer present in Spring Boot 2.x. When using version 2, you need to use a different one:

        @Bean
        @ConditionalOnProperty(name = "external.war.file")
        public TomcatServletWebServerFactory servletContainerFactory(@Value("${external.war.file}") String path,
                                                                     @Value("${external.war.context:}") String contextPath) {
            return new TomcatServletWebServerFactory() {
    
                @Override
                protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
                    new File(tomcat.getServer().getCatalinaBase(), "webapps").mkdirs();
    
                    Context context = tomcat.addWebapp(contextPath, path);
                    context.setParentClassLoader(getClass().getClassLoader());
    
                    return super.getTomcatWebServer(tomcat);
                }
    
            };
        }
    

    Also, Spring boot enbedded Tomcat does not by default contain dependencies for JSPs. If you are using JSPs in your external war, you need to include them.

    
        org.apache.tomcat.embed
        tomcat-embed-jasper
    
    
        javax.servlet
        jstl
    
    

    UPDATE: I've written a more detailed blog post on how to set this up for both Spring Boot 1 and 2.

提交回复
热议问题