springboot基礎一:HelloWorld
緣起
springboot是一個封裝springMvc等相關功能的開發框架,如果要開展新項目,建議使用springboot,一些常用功能都進行了封裝,省去了我們自己去做框架架構,另外springboot的開發效率還是很高的,有幾個朋友還沒有接觸過springboot,恰巧最近有時間,所以做一個springboot的快速入門教程,幾個系列,從spring基礎到springboot整合mybatis,redis,dubbo,elasticsearch等,希望一起進步
HelloWorld
新建一個maven工程
pom文件里加入springboot的parent
<!-- Spring Boot 啟動父依賴 --><parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
依賴里加入springboot web依賴
<!-- Spring Boot web依賴 --> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
新建xxx.xxx.web 包,並在包下建立Application啟動類
/*** Spring Boot應用啟動類
* Created by fqh on 17/6/24.
*/
//springBootApplication註解用來標註此類是springboot啟動類
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
在xxx.xxx.web包下面建一個HelloWorldController類
注意:未來所有web的Controller類必須與Application這個springboot啟動類同包目錄或者在子包目錄裡。
/*** 啟動訪問類rest
* Created by fqh on 17/6/24.
*/
@RestController
public class HelloWorldController {
@RequestMapping("/")
public String sayHello() {
return "Hello,World!";
}
}
運行Application
在java開發工具裡直接右鍵Application,選擇run運行即可啟動項目
. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.4.RELEASE)
..............................
............中間這些信息略.....................
2017-06-24 12:20:38.810 INFO 59808 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-06-24 12:20:38.914 INFO 59808 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-06-24 12:20:38.925 INFO 59808 --- [ main] com.wish.action.web.Application : Started Application in 7.244 seconds (JVM running for 8.982)
可以從啟動裡看到,web項目已經啟動,啟動是用內嵌的Tomcat服務器,端口是默認的8080
測試
在瀏覽器裡輸入:http://localhost:8080/
瀏覽器打印 Hello,World!
很簡單吧。繼續跟著學習吧!