在Spring Boot嵌入式Tomcat中添加war文件

8
我有一个使用嵌入式Tomcat Web服务器和 OpenKM SDK的Spring Boot 2.1.2.RELEASE应用程序。
现在,我有一些集成测试,使用restassured库进行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服务器要长得多,但是它可以进行部署。
然而,部署过程会失败并出现以下错误:
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)

我有这个文件(加上server.xml、context.xml),与openkm.war文件放在一起,但看起来嵌入的tomcat不知道它存在。
所有这些文件都位于src/main/resources/webapp中。
因此,我想知道我缺少哪个配置项或是否有更好的方法来实现我想要的?

我之前也遇到过tmp目录的问题,你可以尝试手动创建“tmp”目录并运行你的服务器。 - Milan Desai
那个目录是存在的。问题在于文件实际上并不存在,所以我认为Tomcat不知道它的存在和如何处理它。 - greengold
1个回答

1

你可以尝试做类似这样的事情;

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/文件夹挂载到{目标挂载路径}上。虽然我不确定这个路径是什么?你能试试用"/",或者"/WEB-INF/"吗?我目前没有办法在本地测试,但希望这会有所帮助。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接