SpringBoot教程(一)建立一個SpringBoot專案
阿新 • • 發佈:2018-12-18
構建簡單的專案
通常微服務都是由一組專案構建而成的一套服務架構,所以為了確保版本的一致性,通常都會用一個父工程來管理jar依賴版本,首先我們建立一個pom作為父工程
構建工程
在父工程下引入springboot依賴包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
在子工程中新增依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter- web</artifactId>
</dependency>
</dependencies>
之後和編寫springmvc是差不多的,但是springboot作為微服務只提供資料和方法,並不關心頁面的跳轉,所以此處不再使用controller註解,而是使用RestController註解,該註解實際上是複合註解,相當於Controller註解 + ResponseBody註解以流形式輸出,而不是交給檢視解析器去轉發頁面. 接下來的編寫和springmvc一樣,方法上加上@RequestMapping註解,我們簡單編寫一個.
@RestController
public class TestController{
@RequestMappin("/test")
public String index(){
return "abcd";
}
}
接下來我們再編寫一個啟動類,需要注意的是 啟動類只能在controller的同包或者父包下,絕對不能放在其他包中,並且該類需要由一個註解來說明是springboot的啟動類 @SpringBootApplication 在main方法中啟動我們的專案 SpringApplication.run
由上圖我們可以看出該專案啟動的預設埠號為8080 我們看訪問一下看看