創(chuàng)建SpringBoot項目
這里是用IDEA做演示,安裝配置好開發(fā)工具,前面有講我是這樣使用SpringBoot(eclipse&IDEA配置JDK Maven docker)。這一章蔣完成SpringBoot項目的創(chuàng)建,開放一個簡單的API接口。通過postman訪問API接口。
創(chuàng)建根目錄
打開IDEA,到創(chuàng)建項目界面。這里創(chuàng)建一個父模板,后面各個項目以子模塊的方式進行。

點擊Create New Project,選中Maven項目,不要選擇Create From archetype,點擊Next

輸入組名(GroupId)、項目名(ArtifactId)、版本號(Version),后點擊next

選擇目錄后點擊Finish

彈出創(chuàng)建目錄的對話框點擊OK

進入IDEA項目界面。

這是parent項目,不需要src。因此,把src目錄刪除。

引入SpringBoot
在parent項目的pom.xml中包含子模塊中都會用到的模塊,如SpringBoot、lombok等。把pom.xml內(nèi)容修改如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.biboheart.demos</groupId>
<artifactId>bhparent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</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>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- 我常用的一些工具寫在這個包里,源碼地址:https://gitee.com/biboheart/brick 已經(jīng)在maven中央倉庫發(fā)布 -->
<dependency>
<groupId>com.biboheart</groupId>
<artifactId>bh-brick</artifactId>
<version>0.0.6</version>
</dependency>
</dependencies>
</project>
當前SpringBoot穩(wěn)定的最新版是2.1.3,我們就用2.1.3來演示吧。

spring boot 官網(wǎng)中選中Learn可以查看。點擊Reference Doc.可以查看spring boot此版本的官方文檔。
這個版本要求Java8及以上版本,Maven 3.3以上
這時,界面狀態(tài)

創(chuàng)建第一個項目
先來一個最基本的項目,只是提供一個API。
-
在bhparent項目名上右鍵->New->Module
右鍵 -
選擇Maven項目,勾選Create from archetype,使用“maven-archetype-puickstart”,點擊“Next”
創(chuàng)建maven模塊 -
填寫模塊ArtifactId為bhhello,GroupId與Version就用默認的,如果有改變,在模塊的pom.xml文件也可以修改。
模塊ArtifactId -
確認信息,Maven的配置項在前面已經(jīng)講過。這里可以修改配置文件。
確認信息 -
模塊名稱。前面(第2步)填寫的是模塊ID,是maven項目屬性,是全局的,如果發(fā)布到中央倉庫,可以用來拉取模塊。這里填的是模塊名稱,也就是項目目錄,是本地的。IDEA默認為模塊ArtifactId,可以修改。這里就不改了。
模塊名稱 -
點擊Finish完成創(chuàng)建。在IDEA右下方查看創(chuàng)建進度。
查看進度
項目結(jié)構(gòu)
一個基本的maven項目的目錄結(jié)構(gòu),是這樣的。在項目根目錄一個pom.xml文件和src文件夾,src文件夾中存放項目源碼。在src目錄下是main和test兩個文件夾,main文件夾下存放主要的項目文件,test文件下存放測試的文件。main和test下的目錄結(jié)構(gòu)一樣,包含一個java目錄和resources目錄。java目錄中存放java源代碼,resources目錄中存放配置文件,資源文件等。一般情況下,test中測試main中的類,使用相同的包名。如圖,com.biboheart.demos。

IDEA默認生成的項目文件名為App和AppTest。這個項目不做測試,刪除test/jave/下的包和文件。main中的App改名成HelloApplication。
IDEA的更名操作,右鍵App文件,Refactor->Rename


完成后項目結(jié)構(gòu)

修改bhhello項目的pom.xml文件,把暫時用不上的去掉。內(nèi)容如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>bhparent</artifactId>
<groupId>com.biboheart.demos</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>bhhello</artifactId>
<name>bhhello</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
</dependencies>
</project>
項目開發(fā)
修改HelloApplication文件內(nèi)容如下
package com.biboheart.demos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class HelloApplication {
public static void main( String[] args ) {
SpringApplication.run(HelloApplication.class, args);
}
@RequestMapping(value = "/hello")
public String hello(String name) {
return "hello " + name;
}
}
在IDEA中運行項目
HelloApplication上右鍵點擊,選擇Debug 'HelloApplication',調(diào)試運行項目

項目開始啟動,在控制臺中會打印出運行信息。


在調(diào)試窗口中看到Undertow服務器已經(jīng)啟動,端口號為8080。項目已經(jīng)完成啟動。點擊左邊的紅色方框可以停止。

測試
瀏覽器中GET測試。

打開postman,或其它http調(diào)試工具。如何安裝postman的問題請通過其它途徑獲取。
用GET方式請求。

用POST方式請求

代碼分析
這個項目已經(jīng)完成,到目前為止,這個項目創(chuàng)建,目錄調(diào)整,maven依賴。進行了一個HelloApplication.java文件的編寫。只是做了少量的開發(fā),就完成了一個API服務。這就是spring boot的強大之處,快速構(gòu)建項目。
下面分析下代碼
package com.biboheart.demos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class HelloApplication {
public static void main( String[] args ) {
SpringApplication.run(HelloApplication.class, args);
}
@RequestMapping(value = "/hello")
public String hello(String name) {
return "hello " + name;
}
}
- @SpringBootApplication
表示這是一個spring boot項目的入口。它包含了一些自動配置的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
- @RestController
表示這是一個RESTful風格的API。配合class中的函數(shù),實現(xiàn)API開放。 - main函數(shù)
public static void main( String[] args ) {
SpringApplication.run(HelloApplication.class, args);
}
使用org.springframework.boot.SpringApplication類的run函數(shù)指定項目入口類,啟動項目。
- @RequestMapping(value = "/hello")
表示開放一個API,URI為“/hello”,API的實現(xiàn)是注解下面的函數(shù)。 - public String hello(String name) {}
實現(xiàn)“/hello”,可以接收名為name的字符串參數(shù)。返回結(jié)果為String類型的字符串。當客戶訪問“http://localhost:8080/hello”時,會找到這個函數(shù)執(zhí)行,并返回執(zhí)行結(jié)果。這里是RESTful風格返回。 - spring boot默認服務端口是8080,可以配置,這個后面章節(jié)會講到。





