简体   繁体   中英

Embeded Tomcat version 9 Not Working to create container for API

I am trying to run tomcat container using dependency 'tomcat-embed-core', version: '9.0.65' . While using this the I am not able to start container, If I move to version 8.5.41 or any version of tomcat-embeded-core 8. *, it is working fine. I am using CFXServlet for servlet. Below is code example.

Package imported:

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;

Code snippet I used:

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();

In build.gradle I have added below dependancy,

// 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' using above dependacy not able to connect http://127.0.0.1:4434/test

If I move to version 8.5.82 its working fine. I have requirement to use tomcat 9 compatibility, So anyone help me on this. Didn't found any error log in app.

Tomcat 9 is not adding a Connector automatically to your server, so you'll have to trigger it yourself using:

tServer.getConnector();

This will add the default connector to server and server start responding So code should be like

Tomcat tServer = new Tomcat();
tServer.setHostname('127.0.0.1');
tServer.setPort(4434);
tServer.getConnector(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM