SpringBoot – 无法启动嵌入式容器
当我启动springboot应用程序时,我的SpringBootLoginController类抛出此错误(无法启动嵌入式容器),如下所示。它是一个hello world类型的spring boot应用程序示例。
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.2.RELEASE) org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean. at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:536) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
我的pom.xml
4.0.0 com.test.springboot HelloSpringBoot 0.0.1-SNAPSHOT war HelloSpringBoot HelloSpringBoot org.springframework.boot spring-boot-starter-parent 1.5.2.RELEASE 1.8 org.springframework.boot spring-boot-starter-web
我的控制器:
import org.springframework.boot.*; import org.springframework.web.bind.annotation.*; @RestController public class SpringBootLoginController { @RequestMapping("/hello") String hello() { return "Hello World!!!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootLoginController.class, args); } }
尝试使用@SpringBootApplication
批注对SpringBootLoginController
类进行批注。
@SpringBootApplication @RestController public class SpringBootLoginController { @RequestMapping("/hello") String hello() { return "Hello World!!!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootLoginController.class, args); } }
通过使用@SpringBootApplication进行批注可以解决此问题。
@SpringBootApplication @RestController public class SpringBootLoginController { @RequestMapping("/hello") String hello() { return "Hello World!!!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootLoginController.class, args); } }
或者,通过添加@EnableAutoConfiguration也可以解决此问题。
@EnableAutoConfiguration @RestController public class SpringBootLoginController { @RequestMapping("/hello") String hello() { return "Hello World!!!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootLoginController.class, args); } }
对于我的情况,我正在使用springboot开发一个命令行项目。
@SpringBootApplication public class Application implements CommandLineRunner { //my code here }
所以我只是使用简单的启动器。
org.springframework.boot spring-boot-starter
但是我也遇到了这个错误,并且我的pom中没有与Web相关的依赖关系。
最后我发现我的一个依赖项目是在自己的pom中使用“javax.servlet.Servlet”。
如果检查springboot的源代码,它将在启动应用程序时检查项目中是否有“javax.servlet.Servlet”。 当有任何“javax.servlet.Servlet”时,尝试启动一个Web“嵌入式容器”。
这就是我收到此错误的原因,因为我使用的是“spring-boot-starter”,并且没有Web容器。
所以解决方案非常简单,只需告诉springboot这不是“application.properties”中的Web项目:
spring.main.web-environment=false
在我的情况下添加
javax.servlet servlet-api 2.5
解决了问题
您应该注释SpringBootLoginController
类。 阅读有关@SpringBootApplication
注释的内容。
@SpringBootApplication @RestController public class SpringBootLoginController { @RequestMapping("/hello") String hello() { return "Hello World!!!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SpringBootLoginController.class, args); } }
如果您正在使用maven,那么构建
mvn package
而不是IDE构建jar。
在我的案例中,IDE构建的jar有很多问题。
你的依赖项有问题。 加上这个
javax.servlet servlet-api