簡單、快速、便捷。java
搭建普通Spring Web項目項目的通常流程:git
.......github
一系列的配置過程繁雜,很容易遺漏掉,即使是隨便搭建一個簡單的爬取某個頁面的郵箱存入數據庫的小項目,都要從頭至尾的將配置流程走一遍。web
若是使用Spring Boot,配置則簡化了不少,只須要引入響應的幾個maven依賴,進行簡單的幾個配置就能夠快速方便的搭建一個Web項目。spring
Dependencies處可經過添加相應的依賴,在生成項目文件時就會自動添加,好比,若是咱們要作Web開發,只要在依賴裏添加Web便可。數據庫
File -> New -> Spring Starter Project新建項目json
配置項目相關基本信息 瀏覽器
選擇依賴,好比Spring Web tomcat
Finish OK,Spring Boot項目構建完成。bash
Tips:
導入或者Myeclipse構建項目有可能會出現pom.xml文件首行出現錯誤:Unknown pom.xml /demo line 1 Maven Configuration Problem。
解決方案:
在pom文件中的節點中加入<maven-jar-plugin.version>3.0.0</maven-jar-plugin.version>而後右鍵項目進入Maven>Update Project...菜單點擊,便可
src/main/java
程序開發以及主程序入口:Applicationsrc/main/resources
配置文件存放位置:application.propertiessrc/test/java
測試入口:ApplicationTests生成的Application
和ApplicationTests
類均可以直接運行來啓動當前建立的項目,因爲目前該項目未配合任何數據訪問或Web模塊,程序會在加載完Spring以後結束運行。
spring-boot-starter-parent指定了當前項目爲一個Spring Boot項目,它提供了諸多的默認Maven依賴。
Spring Boot提供了許多開箱即用的依賴模塊,這些模塊都是以spring-boot-starter-XX命名的。好比要開啓Spring Boot的web功能,只須要在pom.xml中配置spring-boot-starter-web便可:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
複製代碼
由於其依賴於spring-boot-starter-parent,因此這裏能夠不用配置version。
pom.xml 文件中默認有兩個模塊:
spring-boot-starter
:核心模塊,包括自動配置支持、日誌和 YAML,若是引入了 spring-boot-starter-web
web 模塊能夠去掉此配置,由於 spring-boot-starter-web
自動依賴了 spring-boot-starter
。spring-boot-starter-test
:測試模塊,包括 JUnit、Hamcrest、Mockito。package com.w3cjava.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@RequestMapping("/hello")
public String index() {
return "Hello World";
}
}
複製代碼
@RestController
至關於Spring中的@Controller和@ResponseBody組合使用的,直接以 json 格式輸出。
<!-- 支持web的模塊依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排除tomcat依賴 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
複製代碼
<!-- jetty依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
複製代碼
模擬對 http://localhost:8080/hello 發送請求測試
package com.w3cjava;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.w3cjava.controller.HelloWorldController;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
private MockMvc mvc;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build();
}
@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Hello World")));
}
}
複製代碼
<!-- 熱部署依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
複製代碼
@RequestMapping("/hello1")
public String index1() {
return "Hello World1";
}
複製代碼
spring-boot-maven-plugin:可以以Maven的方式爲應用提供Spring Boot的支持,即爲Spring Boot應用提供了執行Maven操做的可能。
Spring Boot Maven plugin的5個Goals
整體上而言,經過Spring Boot能夠快速構建項目,若是須要使用某個特定的功能,只要添加對應的依賴及簡單配置項便可。
歡迎掃面下列二維碼關注「餘弦的自留地」公衆微信號