【发布时间】:2022-09-26 17:04:12
【问题描述】:
我正在尝试使用依赖项运行 tomcat 容器\'tomcat-embed-core\',版本:\'9.0.65\'. 使用它时,我无法启动容器,如果我移至版本8.5.41或任何版本的tomcat-embed-core 8.*,它工作正常。我将 CFXServlet 用于 servlet。下面是代码示例。
导入的包:
import java.io.File;
import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.apache.tomcat.util.descriptor.web.FilterDef;
import org.apache.tomcat.util.descriptor.web.FilterMap;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
我使用的代码 sn-p :
Tomcat tServer = new Tomcat();
tServer.setHostname(\'127.0.0.1\');
tServer.setPort(4434);
tServer.getHost().setAppBase(\".\");
Context ctx = tServer.addContext(\"/\", new File(\".\").getAbsolutePath());
tServer.addServlet( ctx, CXFServlet.class.getSimpleName(), CXFServlet.class.getName() );
tServer.getHost().setAutoDeploy(true);
tServer.getHost().setDeployOnStartup(true);
ctx.addServletMappingDecoded(\"/test/*\", CXFServlet.class.getSimpleName());
ctx.addApplicationListener(ContextLoaderListener.class.getName());
ctx.addParameter(\"contextClass\",AnnotationConfigWebApplicationContext.class.getName());
ctx.addParameter(\"contextConfigLocation\", RestConfig.class.getName());
Class filterClass = DelegatingFilterProxy.class;
FilterDef myFilterDef = new FilterDef();
myFilterDef.setFilterClass(filterClass.getName());
myFilterDef.setFilterName(\"springSecurityFilterChain\");
ctx.addFilterDef(myFilterDef);
FilterMap myFilterMap = new FilterMap();
myFilterMap.setFilterName(\"springSecurityFilterChain\");
myFilterMap.addURLPattern(\"/*\");
ctx.addFilterMap(myFilterMap);
tServer.start();
tServer.getServer().await();
在 build.gradle 我添加了以下依赖项,
// https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core implementation group: \'org.apache.tomcat.embed\', name: \'tomcat-embed-core\', version: \'9.0.60\'
使用上述依赖项无法连接http://127.0.0.1:4434/test
如果我移至 8.5.82 版,它工作正常。 我需要使用 tomcat 9 兼容性,所以任何人都可以帮助我。 在应用程序中未发现任何错误日志。
-
我建议转移到 Spring Boot 并使用该功能来运行嵌入式服务器,而不是自己发明。
-
我同意@M.Deinum 的观点,即使用标准而不是试图发明自己的解决方案通常是一个好主意。如果由于某种原因这不是您的选择,那么添加您从构建/运行/等获得的日志错误消息。对我们帮助您来说将是一个巨大的帮助:)
-
实际上我们正在使用 Spring Boot 应用程序,但是我们对此有一些特定的要求,@RohdeFischer Actully 我没有得到任何日志,而且我不知道启用它的日志。运行时它在任何地方都没有得到标准日志并试图通过 netstat 查看端口但端口未启动
标签: java spring gradle tomcat9