1. 程式人生 > >springboot java.util.concurrent.ExecutionException

springboot java.util.concurrent.ExecutionException

今天嘗試使用SpringBoot部署專案,使用官網的一個小demo(http://projects.spring.io/spring-boot/)

pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.5.RELEASE</version>
</parent>
<dependencies>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import
org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; @Controller @EnableAutoConfiguration public class SampleController { @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } public static void main(String[] args) throws
Exception { SpringApplication.run(SampleController.class, args); } }

啟動報錯:

2017-04-07 09:52:50.325 INFO 10640 --- [ main] springboot.SampleController : Starting SampleController on jiangshubian with PID 10640 (D:\IntelliJ_IDEA_Project\MavenTest\target\classes started by 123 in D:\IntelliJ_IDEA_Project\MavenTest)
2017-04-07 09:52:50.332 INFO 10640 --- [ main] springboot.SampleController : No active profile set, falling back to default profiles: default
2017-04-07 09:52:50.611 INFO 10640 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot[email protected]410676f3: startup date [Fri Apr 07 09:52:50 CST 2017]; root of context hierarchy
2017-04-07 09:52:55.600 INFO 10640 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-04-07 09:52:55.616 INFO 10640 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-04-07 09:52:55.618 INFO 10640 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.37
2017-04-07 09:52:55.851 INFO 10640 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-04-07 09:52:55.851 INFO 10640 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 5245 ms
2017-04-07 09:52:56.816 INFO 10640 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-04-07 09:52:56.828 ERROR 10640 --- [cat-startStop-1] org.apache.catalina.core.ContainerBase : A child container failed during start

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]]
at java.util.concurrent.FutureTask.report(FutureTask.java:122) [na:1.7.0_80]
at java.util.concurrent.FutureTask.get(FutureTask.java:188) [na:1.7.0_80]
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:915) ~[tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871) [tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145) [tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407) [tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397) [tomcat-embed-core-8.0.37.jar:8.0.37]
at java.util.concurrent.FutureTask.run(FutureTask.java:262) [na:1.7.0_80]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_80]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_80]
at java.lang.Thread.run(Thread.java:745) [na:1.7.0_80]
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:162) [tomcat-embed-core-8.0.37.jar:8.0.37]
... 6 common frames omitted
Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.addServlet(Ljava/lang/String;Ljavax/servlet/Servlet;)Ljavax/servlet/ServletRegistration$Dynamic;
at org.springframework.boot.context.embedded.ServletRegistrationBean.onStartup(ServletRegistrationBean.java:190) ~[spring-boot-1.3.8.RELEASE.jar:1.3.8.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.selfInitialize(EmbeddedWebApplicationContext.java:225) ~[spring-boot-1.3.8.RELEASE.jar:1.3.8.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.access$000(EmbeddedWebApplicationContext.java:85) ~[spring-boot-1.3.8.RELEASE.jar:1.3.8.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext$1.onStartup(EmbeddedWebApplicationContext.java:209) ~[spring-boot-1.3.8.RELEASE.jar:1.3.8.RELEASE]
at org.springframework.boot.context.embedded.tomcat.TomcatStarter.onStartup(TomcatStarter.java:55) ~[spring-boot-1.3.8.RELEASE.jar:1.3.8.RELEASE]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303) ~[tomcat-embed-core-8.0.37.jar:8.0.37]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145) [tomcat-embed-core-8.0.37.jar:8.0.37]
... 6 common frames omitted

2017-04-07 09:52:56.831 ERROR 10640 --- [ main] org.apache.catalina.core.ContainerBase : A child container failed during start

--原因是在pom.xml中引入了:servlet-api 2.5版本導致,去掉後正常;

springboot depends on servlet3 or higher, so you should remove servlet-api-2.5:jar and other lower than 3.0 versions.

但是使用3.0-alpha-1發覺也不行。

<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>3.0-alpha-1</version>
</dependency>