Code works with Embedded Apache Tomcat 8 but not with 9. What's changed?

后端 未结 1 662
生来不讨喜
生来不讨喜 2020-12-06 17:43

Embedding Apache Tomcat into an eclipse web app project.
The code works when I\'m using the latest Tomcat 8 (8.0.5 Embedded) jars as dependencies, and this server respon

相关标签:
1条回答
  • 2020-12-06 18:12

    It looks like you haven't added a Connector to your embedded server. Tomcat 9 no longer automatically adds a Connector to your server for you, so you'll have to trigger it yourself:

    package app;
    
    import org.apache.catalina.LifecycleException;
    import org.apache.catalina.startup.Tomcat;
    
    public class Main {
    
        public static void main(String[] args) {
    
            Tomcat tomcat = new Tomcat();
            tomcat.setPort(8080);
            tomcat.getConnector(); // Trigger the creation of the default connector
    
            try {
                tomcat.start();
            } catch (LifecycleException e) {
                e.printStackTrace();
            }
    
            tomcat.getServer().await();
        }
    }
    

    It's worth mentioning that adding a call to tomcat.getConnector() should be safe for previous versions of Tomcat as well, so this need not be a "Tomcat 9-only" thing.

    0 讨论(0)
提交回复
热议问题