向Spring Boot嵌入式tomcat添加战争

gre*_*old 7 java spring embedded-tomcat-7 spring-boot openkm

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

现在,我有一些集成测试,这些测试使用restassuredlib进行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;
    }
}
Run Code Online (Sandbox Code Playgroud)

与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)
Run Code Online (Sandbox Code Playgroud)

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

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

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

bur*_*ete 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);
Run Code Online (Sandbox Code Playgroud)

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

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