開發(fā)環(huán)境
本次教程的編寫環(huán)境:
操作系統(tǒng):
Mac OS X 10.12.4

操作系統(tǒng)
ide:
IntelliJ IDEA 2017.1.1
Build #IU-171.4073.35, built on April 7, 2017
JRE:
1.8.0_112-release-736-b16 x86_64
JVM:
OpenJDK 64-Bit Server VM by JetBrains s.r.o
準(zhǔn)備工作
將以下代碼加入idea的live template,命名為springbootStartup
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Create New Project

Create New Project - 1

Create New Project - 2

Create New Project - 3

Create New Project - 4

Create New Project - 5

Create New Project - 6
添加maven支持
添加pom.xml
在project的根目錄上的右鍵菜單中選擇"Add Framework Support",添加maven支持。

添加maven支持 - 1

添加maven支持 - 2

添加maven支持 - 3
設(shè)置maven坐標(biāo)

添加maven支持 - 4
補(bǔ)全maven其他設(shè)置
在pom.xml中,坐標(biāo)設(shè)置下面輸入sp,IDE自動彈出前面設(shè)置的live template:“springbootStartup”,按下"tab"鍵盤,剩余的maven代碼將會自動補(bǔ)全。

添加maven支持 - 5

添加maven支持 - 6
新建包結(jié)構(gòu)

新建包結(jié)構(gòu)
新建application類

新建application類 - 1

新建application類 - 2

新建application類 - 3

新建application類 - 4

新建application類 - 5
在main函數(shù)中添加如下代碼:
SpringApplication.run(SpringBootDemoApplication.class, args);
添加controller類

添加controller類 - 1
添加@RestController

添加controller類 - 2
添加request mapping代碼
@RequestMapping("/hello")
String hello() {
return "this is a test";
}

添加controller類 - 3
測試

測試 - 1
第一次啟動報錯,顯示8080端口已經(jīng)被占用

測試 - 2
因此需要修改端口號,操作如下:
在resources目錄下新建application.properties, 輸入如下內(nèi)容:
server.port=8081
然后重新啟動程序,在瀏覽器中訪問地址:
http://localhost:8081/hello

測試 -