一、操作步驟
二、詳細步驟

2、選擇Spring Initializr 然后Next
3、輸入Artiface 然后Next
4、勾選Web 、模版我們選擇官方推薦的Thymeleaf模版引擎,其他框架、中間件、數據庫根據需要選擇即可,而且無需我們手動去添加配置文件等,選擇完成后Next
選擇模版引擎
5、Finish即可
6、查看Spring Boot項目結構目錄
7、在pom.xml添加如下內容
注意:如果新建項目時選擇了依賴的mybatis、mongodb之類的啟動時候會報錯,由於沒配置數據源及mongodb的連接信息,此時如果只是想測試項目是否搭建成功先注釋即可
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--<dependency>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter-data-mongodb</artifactId>--> <!--</dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--<dependency>--> <!--<groupId>org.mybatis.spring.boot</groupId>--> <!--<artifactId>mybatis-spring-boot-starter</artifactId>--> <!--<version>1.3.2</version>--> <!--</dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
8、編寫Hello Spring Boot的Controller
package com.example.bootopen.com; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloSpringBootController { @RequestMapping("/hello") public String hello() { return "Hello Spring Boot"; } }
注意:如果只是簡單測試項目只需要添加端口即可,其他數據源、緩存、靜態資源路徑也可以在此配置。
筆者推薦一種配置文件模式:另外新建2個配置文件,一個開發環境,一個線上環境,通過application.properties自由切換
10、啟動項目 選擇Run、Debug啟動
關於@SpringBootApplication注解說明:@SpringBootApplication開啟了Spring的組件掃描和springboot的自動配置功能,相當於將以下三個注解組合在了一起
(1)@Configuration:表名該類使用基於Java的配置,將此類作為配置類
(2)@ComponentScan:啟用注解掃描
(3)@EnableAutoConfiguration:開啟springboot的自動配置功能
訪問項目 http://localhost:8089/hello