Spring Boot右鍵maven build成功但是直接運(yùn)行main方法出錯(cuò)的解決方案
1、代碼就一個(gè)Controller,從官網(wǎng)復(fù)制過(guò)來(lái)的,如下
package com.springboot.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/index")
@ResponseBody
String home() {
return "Hello World";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
2、在項(xiàng)目上右鍵,maven build,輸入 spring-boot:run,過(guò)幾秒后控制臺(tái)能看見(jiàn)success,也能看見(jiàn)Hello World,但是沒(méi)有傳說(shuō)中的那個(gè)用字符拼拼出來(lái)的spring圖案,而且http://localhost:8080/也打不開(kāi),于是我機(jī)智的在上面的SampleController類中右鍵->java Application,果真,出錯(cuò)了,還more than 18... 錯(cuò)誤如下:
1 Cannot instantiate interface org.springframework.boot.SpringApplicationRunListener : org.springframework.boot.context.event.EventPublishingRunListener
等等之類的,就是找不到類的error
3、我的解決辦法
之前我的pom.xml:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> </parent>
百度、搜狗、谷歌找了2個(gè)小時(shí)的方法,自己手動(dòng)引入其他dependency等等都不行,但是更改了springboot的版本就好了,更改后如下:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.7.RELEASE</version> </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]
打開(kāi)http://localhost:8080/也能看見(jiàn)我的Hello World

仔細(xì)觀察了最后一句,應(yīng)該還有一些問(wèn)題。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 只需兩步實(shí)現(xiàn)Eclipse+Maven快速構(gòu)建第一個(gè)Spring Boot項(xiàng)目
- maven+springboot打成jar包的方法
- spring-boot-maven-plugin 插件的作用詳解
- SpringBoot+Maven 多模塊項(xiàng)目的構(gòu)建、運(yùn)行、打包實(shí)戰(zhàn)
- Spring Boot+maven打war包的方法
- 利用Maven入手Spring Boot第一個(gè)程序詳解
- Spring Boot Maven 打包可執(zhí)行Jar文件的實(shí)現(xiàn)方法
- Spring Boot maven框架搭建教程圖解
相關(guān)文章
ThreadLocal線程在Java框架中的應(yīng)用及原理深入理解
這篇文章主要介紹了ThreadLocal在Java框架中的應(yīng)用及原理深入理解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
MyBatis動(dòng)態(tài)<if>標(biāo)簽使用避坑指南
這篇文章主要為大家介紹了MyBatis動(dòng)態(tài)<if>標(biāo)簽使用避坑指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
教你如何使用Java8實(shí)現(xiàn)菜單樹(shù)形數(shù)據(jù)
今天給大家?guī)?lái)的是關(guān)于JAVA的相關(guān)知識(shí),文中圍繞著如何使用Java8實(shí)現(xiàn)菜單樹(shù)形數(shù)據(jù)展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Spring事件監(jiān)聽(tīng)器ApplicationListener源碼詳解
這篇文章主要介紹了Spring事件監(jiān)聽(tīng)器ApplicationListener源碼詳解,ApplicationEvent以及Listener是Spring為我們提供的一個(gè)事件監(jiān)聽(tīng)、訂閱的實(shí)現(xiàn),內(nèi)部實(shí)現(xiàn)原理是觀察者設(shè)計(jì)模式,需要的朋友可以參考下2023-05-05
java實(shí)現(xiàn)簡(jiǎn)單斗地主(看牌排序)
這篇文章主要介紹了java實(shí)現(xiàn)簡(jiǎn)單斗地主,看牌進(jìn)行排序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2010-11-11

