7

我有一个使用嵌入式 tomcat 网络服务器并通过它的 SDK使用OpenKM的 spring-boot 2.1.2.RELEASE 应用程序。

现在,我有一些使用restassuredlib 进行 REST 调用和验证响应结构的集成测试。我的想法是集成OpenKM.war到这个 wmbedded 的 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;
    }
}

deploy 比在 openkm 附带的独立 tomcat webserver 上花费的时间更长,但它可以部署。

但是部署过程失败:

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.

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

4

1 回答 1

0

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

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/"?我目前无法在本地进行测试,但我希望这会有所帮助。

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

于 2019-10-16T07:27:41.270 回答