我有一个spring-boot 2.1.2.RELEASE应用程序,它使用嵌入式tomcat SDK服务器,并通过它的SDK使用OpenKM。
现在,我有一些使用restassured库进行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;
}
}部署比在openkm附带的独立tomcat way服务器上更长,但它可以部署。
但是,部署过程会失败,出现以下错误:
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中。
所以,我想知道我遗漏了什么配置,或者是否有更好的配置来实现我想要的?
发布于 2019-10-16 15:27:41
你可以试着这样做:
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挂载单独的文件。
https://stackoverflow.com/questions/56114225
复制相似问题