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

来源:互联网 发布:国家统计局2016年数据 编辑:程序博客网 时间:2024/05/20 00:16

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

<dependencies>    <!-- spring-boot使用自带tomcat容器配置begin -->    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-tomcat</artifactId>        <scope>provided</scope>    </dependency>    <dependency>        <groupId>org.apache.tomcat.embed</groupId>        <artifactId>tomcat-embed-jasper</artifactId>        <scope>provided</scope>    </dependency>    <dependency>        <groupId>org.apache.tomcat</groupId>        <artifactId>tomcat-juli</artifactId>        <version>1.8</version>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>        <version>1.5.3.RELEASE</version>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>        <version>1.5.3.RELEASE</version>        <scope>test</scope>    </dependency>    <!-- spring-boot使用自带tomcat容器配置end --></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 omittedCaused by: java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory    at org.apache.catalina.util.LifecycleBase.<clinit>(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 omittedCaused 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>    <!-- spring-boot使用jetty容器配置begin -->    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>        <!-- 排除默认的tomcat,引入jetty容器. -->        <exclusions>           <exclusion>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-tomcat</artifactId>           </exclusion>        </exclusions>    </dependency>    <!-- jetty 容器. -->    <dependency>          <groupId>org.springframework.boot</groupId>          <artifactId>spring-boot-starter-jetty</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-test</artifactId>        <scope>test</scope>    </dependency>    <!-- spring-boot使用jetty容器配置end --></dependencies>

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

阅读全文
0 0