根據我們程序員學習的慣例,學習一門新技術都是從HelloWorld開始的。 感受編程是一件很是富有意義的事情,程序員也是一羣可愛的人,渴望被關懷和關注,由於咱們總在和世界say Hi. 好了進入正題java
首先建立一個項目,可看我上一篇文章寫得 IntelliJ IDEA建立第一個Spring boot項目 接下來運行這個項目,你將會看到以下頁面 git
在項目名/src/main/java/包名下,新建一個config包,包下面建立HelloController程序員
@Controller
public class HelloController {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
@ResponseBody
public String hello(){
return "Hello World";
}
}
註解說明:
@Controller: 可以讓項目掃描自動檢測到這個類,處理http請求
@ RequestMapping 請求的路由映射,訪問的路徑就是:http://localhost:8080/hello
value: 路由名
method: 請求方式,GET,POST,PUT,DELETE等
複製代碼
訪問:http://localhost:8080/hello, 就看到Hello World了
複製代碼
父項目github
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
管理Spring Boot應用裏面所依賴的版本
複製代碼
管理依賴web
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Spring Boot將全部的功能場景都抽取出來,作成一個個的starters(啓動器),只須要在項目裏面引入這些starter相關場景的全部依賴都會導入進來,要用什麼功能就導入什麼場景的啓動器
複製代碼
個人網站:wayne214.github.iospring