spring boot 報錯 Unable to start embedded container;

想做個rest風格的控制器,簡單例子:

 

[html] view plain copy
  1. @RestController  
  2. @RequestMapping("/user")  
  3. public class UserController {  
  4.   
  5.     @RequestMapping("/{id}")    
  6.     public User view(@PathVariable("id") Long id){  
  7.         User user = new User();  
  8.         user.setId(id);  
  9.         user.setName("linli");  
  10.           
  11.         return user;  
  12.     }  
  13.   
  14.       
  15.     public static void main(String[] args) {  
  16.         // TODO Auto-generated method stub  
  17.       SpringApplication.run(UserController.class,args);  
  18.         //System.out.println("sss");  
  19.     }  
  20.       
  21. }  


啓動的時候報錯:

[html] view plain copy
  1.   .   ____          _            __ _ _  
  2.  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \  
  3. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \  
  4.  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )  
  5.   '  |____| .__|_| |_|_| |_\__, | / / / /  
  6.  =========|_|==============|___/=/_/_/_/  
  7.  :: Spring Boot ::        (v1.3.1.RELEASE)  
  8.   
  9. 2015-12-22 09:30:23.579  INFO 3208 --- [           main] controller.UserController                : Starting UserController on IQSZ-D2678 with PID 3208   
[html] view plain copy
  1. 2015-12-22 09:30:23.582  INFO 3208 --- [           main] controller.UserController                : No active profile set, falling back to default profiles: default  
  2. 2015-12-22 09:30:23.879  INFO 3208 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@12ceb70: startup date [Tue Dec 22 09:30:23 CST 2015]; root of context hierarchy  
  3. 2015-12-22 09:30:24.391  WARN 3208 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 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.  
  4. 2015-12-22 09:30:25.211 ERROR 3208 --- [           main] o.s.boot.SpringApplication               : Application startup failed  
  5.   
  6. 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.  
  7.     at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]  
  8.     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]  
  9.     at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]  
  10.     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:764) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]  
  11.     at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:357) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]  
  12.     at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]  
  13.     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1124) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]  
  14.     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1113) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]  
  15.     at controller.UserController.main(UserController.java:28) [classes/:na]  
  16. Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.  
  17.     at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:185) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]  
  18.     at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]  
  19.     at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]  
  20.     ... 8 common frames omitted  

 

報的是  無法啓動嵌入容器,工廠類不存在。

在類頭加上  @EnableAutoConfiguration。就OK了。這個接口看名字是允許自動配置。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章