繁体   English   中英

向Spring Boot嵌入式tomcat添加战争

[英]Add war to spring boot embedded tomcat

我有一个spring-boot 2.1.2.RELEASE应用程序,该应用程序使用嵌入式tomcat Web服务器并通过其SDK使用OpenKM

现在,我进行了一些集成测试,这些测试使用可restassured lib进行REST调用并验证响应结构。 我的想法是将OpenKM.war集成到此嵌入式tomcat中,并能够运行此测试,而无需在其他服务器上运行openkm应用程序。

这就是我使嵌入式tomcat读取和部署openkm.war的方式:

@Configuration
public class TomcatConfig {
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }

            @Override
            protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
                new File(tomcat.getServer().getCatalinaBase(), "webapp").mkdirs();
                try {
                    tomcat.addWebapp("/okm", new ClassPathResource("webapp/openkm.war").getFile().toString());
                } catch (Exception ex) {
                    throw new IllegalStateException("Failed to add okm", ex);
                }
                return super.getTomcatWebServer(tomcat);
            }
        };

        tomcat.addAdditionalTomcatConnectors(redirectConnector());
        return tomcat;
    }

    private Connector redirectConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8080);
        connector.setSecure(false);
        connector.setRedirectPort(8443);
        return connector;
    }
}

与openkm附带的独立tomcat Web服务器上相比,deploy需要更长的时间,但是它可以部署。

但是,部署过程因以下原因而失败:

IOException parsing XML document from URL [file:/tmp/tomcat.2352216859213135410.8080/openkm.xml]; nested exception is java.io.FileNotFoundException: /tmp/tomcat.2352216859213135410.8080/openkm.xml (No such file or directory)

我在openkm.war文件旁边有这个文件(加上server.xml,context.xml),但似乎嵌入式tomcat对此一无所知。

所有这些文件都位于src/main/resources/webapp

因此,我想知道我缺少什么配置,或者是否还有其他更好的方法可以实现我想要的?

您可以尝试做类似的事情;

StandardContext ctx = (StandardContext) tomcat.addWebapp("/okm", new ClassPathResource("webapp/openkm.war").getFile().toString());
WebResourceRoot resources = new StandardRoot(ctx);
resources.addPreResources(new DirResourceSet(resources, "{target mount path}",
        new File("src/main/resources/webapp").getAbsolutePath(), "/"));
ctx.setResources(resources);

这样您就可以在Tomcat部署中将src/main/resources/webapp/文件夹安装在{target mount path}上。 虽然我不确定这条路吗? 您可以尝试使用"/" ,也可以尝试使用"/" "/WEB-INF/"吗? 我目前无法在本地进行测试,但我希望这会有所帮助。

如果需要在tomcat中不同文件夹中的其他文件,也可以使用FileResourceSet挂载单个文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM