第一步:新建一個maven項目
- 新建項目,選擇maven
- 填寫GroupId和ArtifactId
- 下一步默認即可,直接點擊finish
- 創建完成後項目結構如下
第二步: 配置pom.xml
在pom.xml中添加如下代碼:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- parent指定項目的父項目是spring-boot的版本管理中心,因爲有了這個,spring-boot項目中導入其他spring的jar包時,就不再需要指定版本號。
- spring-boot-starter-web 是啓動器,幫我們導入了web模塊正常運行所需要的所有依賴的組件。
第三步:添加主程序類
新建DemoMainApplication.class:
內容如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoMainApplication {
public static void main(String[] args) {
SpringApplication.run(DemoMainApplication.class,args);
}
}
第四步:添加controller
在DemoMainApplication.class的所在目錄創建controller文件夾並創建DemoController.class
內容如下:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@RequestMapping("/hello")
public String Hello(){
return "Hello World!";
}
}
第五步:啓動
日誌出現如下日誌說明啓動成功:
瀏覽器中訪問:http://localhost:8080/hello