1、代碼就一個Controller,從官網復制過來的,如下
1 package com.springboot.controller; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.*; 7 8 @Controller 9 @EnableAutoConfiguration 10 public class SampleController { 11 12 @RequestMapping("/index") 13 @ResponseBody 14 String home() { 15 return "Hello World"; 16 } 17 18 public static void main(String[] args) throws Exception { 19 SpringApplication.run(SampleController.class, args); 20 } 21 }
2、在項目上右鍵,maven build,輸入 spring-boot:run,過幾秒后控制台能看見success,也能看見Hello World,但是沒有傳說中的那個用字符拼拼出來的spring圖案,而且http://localhost:8080/也打不開,於是我機智的在上面的SampleController類中右鍵->java Application,果真,出錯了,還more than 18... 錯誤如下:
1 Cannot instantiate interface org.springframework.boot.SpringApplicationRunListener : org.springframework.boot.context.event.EventPublishingRunListener
等等之類的,就是找不到類的error
3、我的解決辦法
之前我的pom.xml:
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>1.5.8.RELEASE</version> 5 </parent>
百度、搜狗、谷歌找了2個小時的方法,自己手動引入其他dependency等等都不行,但是更改了springboot的版本就好了,更改后如下:
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>1.4.7.RELEASE</version> 5 </parent>
4、最后在SampleController類中右鍵->java Application,終於再console中輸出了:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.4.7.RELEASE) 2017-11-03 16:17:14.954 INFO 6416 --- [ main] c.s.controller.SampleController : Starting SampleController on USER-20170626MT with PID 6416 (D:\j2ee_workspace\SpringTest\target\classes started by Administrator in D:\j2ee_workspace\SpringTest) 2017-11-03 16:17:14.956 INFO 6416 --- [ main] c.s.controller.SampleController : No active profile set, falling back to default profiles: default 2017-11-03 16:17:15.005 INFO 6416 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@411f53a0: startup date [Fri Nov 03 16:17:15 CST 2017]; root of context hierarchy 2017-11-03 16:17:16.688 INFO 6416 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2017-11-03 16:17:16.702 INFO 6416 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
打開http://localhost:8080/也能看見我的Hello World
仔細觀察了最后一句,應該還有一些問題。
ps:如果你也遇見類似的問題,不要放棄,堅持,一定能解決的。由此紀念我的這個下午,好了,我開測了~