Skip to content

Load multiple web applications/clients from within a standalone @SpringBootApplication not working #8239

@ghost

Description

Hi!

I am currently trying to load an additional web application during the startup of a standalone @SpringBootApplication. I'm running into several problems but one of them is as I run the Java application from within Eclipse, I get a lot of exceptions getting thrown at me by the embedded Tomcat server (see full output log).

I am not sure why I can do about this and I'm stuck for quite a while now for such a "simple" task. I have a related question on stackoverflow if you want to take a look at it.

I have the web application as a WAR file inside the standalone as a resource. As the application starts/loads I want to take this resource and move/copy it to the server-home directory. From there I want to call Tomcat#addWebapp - and like I said - I was able to make this work for the final standalone jar but I'm not able to run this in Eclipse which means I cannot debug the actual server code.

In the end it's boiling down to these few lines of code:

@SpringBootApplication
public class Server {

    public static void main(String[] args) {
        SpringApplication.run(EasyModelAccessServer.class, args);
    }

    @Bean
    public EmbeddedServletContainerFactory servletContainerFactory() {

        return new TomcatEmbeddedServletContainerFactory() {

            @Override
            protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {

                String appHome = System.getProperty(Environment.APP_HOME);
                String targetFileName = "web-0.0.1-SNAPSHOT.war";
                InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(targetFileName);

                File target = new File(Paths.get(appHome, targetFileName).toString());

                try {

                    java.nio.file.Files.copy(resourceAsStream, target.getAbsoluteFile().toPath(), StandardCopyOption.REPLACE_EXISTING);

                    tomcat.addWebapp("/web", target.getAbsolutePath());

                } catch (ServletException ex) {
                    throw new IllegalStateException("Failed to add webapp", ex);
                } catch (Exception e) {
                    throw new IllegalStateException("Failed to add webapp", e);
                }
                
                return super.getTomcatEmbeddedServletContainer(tomcat);
            }
        };
    }

}

Can anybody tell me how I can do this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions