spring-boot项目启动失败的一种解决办法:自带的tomcat容器切换成jetty容器

按照spring-boot官方的demo例子好像搭建一个spring-boot工程非常简单,但是环境的差异导致工程启动失败,这却让人异常蛋疼!
比如刚开始使用下面这个配置启动就挂了,在自己本地环境启动直接失败报红叉:
spring-boot使用自带tomcat容器的配置:

<dependencies>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-tomcatartifactId>
        <scope>providedscope>
    dependency>
    <dependency>
        <groupId>org.apache.tomcat.embedgroupId>
        <artifactId>tomcat-embed-jasperartifactId>
        <scope>providedscope>
    dependency>
    <dependency>
        <groupId>org.apache.tomcatgroupId>
        <artifactId>tomcat-juliartifactId>
        <version>1.8version>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
        <version>1.5.3.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <version>1.5.3.RELEASEversion>
        <scope>testscope>
    dependency>
    
dependencies>

由于上面的配置使用的容器是spring-boot自带的tomcat容器,导致启动报错:

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.NoClassDefFoundError: org/apache/catalina/SessionIdGenerator
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:536) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at com.example.demo.DemoApplication.main(DemoApplication.java:13) [classes/:na]
Caused by: java.lang.NoClassDefFoundError: org/apache/catalina/SessionIdGenerator
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.prepareContext(TomcatEmbeddedServletContainerFactory.java:192) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:178) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:164) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    ... 8 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
    at org.apache.catalina.util.LifecycleBase.(LifecycleBase.java:37) ~[tomcat-embed-core-7.0.53.jar:7.0.53]
    at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:169) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:164) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    ... 8 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_101]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_101]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_101]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_101]
    ... 12 common frames omitted

在度娘上搜了一圈,spring-boot启动的这些异常,有些说是tomcat版本冲突的问题所致,但查看我的spring工程,tomcat版本没有问题,调试了一圈,搞的晕头转向,最后干脆不搞tomcat,直接改用jetty算了!

解决办法:使用嵌入式jetty作为web容器解决这些问题,省事又省心(根据项目需要选择jetty或者tomcat最为web容器)

<dependencies>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
        
        <exclusions>
           <exclusion>
             <groupId>org.springframework.bootgroupId>
             <artifactId>spring-boot-starter-tomcatartifactId>
           exclusion>
        exclusions>
    dependency>
    
    <dependency>
          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-starter-jettyartifactId>
    dependency>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-testartifactId>
        <scope>testscope>
    dependency>
    
dependencies>

至于jetty容器与tomcat容器的特性和优缺点,请自行度娘了解!

你可能感兴趣的:(spring-boot)