Spring Boot 菜鳥教程 1 HelloWorld
阿新 • • 發佈:2019-02-20
GitHub
技能要求
- 最好對Spring有一定認識
- 最好對Maven有一定認識
簡介
Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Spring Boot致力於在蓬勃發展的快速應用開發領域(rapid application development)成為領導者。
功能
- 建立獨立的Spring applications
- 能夠使用內嵌的Tomcat, Jetty or Undertow,不需要部署war
- 提供starter pom來簡化maven配置
- 自動配置Spring
- 提供一些生產環境的特性,比如metrics, health checks and externalized configuration
- 絕對沒有程式碼生成和XML配置要求
開篇
- 如果你用過Spring JavaConfig的話,會發現雖然沒有了xml配置的繁瑣,但是使用各種註解匯入也是很大的坑,
- 然後在使用一下Spring Boot,你會有一縷清風拂過的感覺,
- 最後真是爽的不得了。。。
專案結構圖
核心註解類說明
@RestController
就是@Controller+@ResponseBody組合,支援RESTful訪問方式,返回結果都是json字串
@SpringBootApplication
就是@SpringBootConfiguration+@EnableAutoConfiguration+
@ComponentScan等組合在一下,非常簡單,使用也方便
@SpringBootTest
Spring Boot版本1.4才出現的,具有Spring Boot支援的載入程式(例如,載入應用程式、屬性,為我們提供Spring Boot的所有精華部分)
關鍵是自動匯入測試需要的類。。。
配置檔案pom.xml
<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.jege.spring.boot</groupId>
<artifactId>spring-boot-hello-world</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-hello-world</name>
<url>http://maven.apache.org</url>
<!-- 公共spring-boot配置,下面依賴jar檔案不用在寫版本號 -->
<parent>
<groupId>org.springframework.boot</groupId>
<!-- 自動包含以下資訊: -->
<!-- 1.使用Java6編譯級別 -->
<!-- 2.使UTF-8編碼 -->
<!-- 3.實現了通用的測試框架 (JUnit, Hamcrest, Mockito). -->
<!-- 4.智慧資源過濾 -->
<!-- 5.智慧的外掛配置(exec plugin, surefire, Git commit ID, shade). -->
<artifactId>spring-boot-starter-parent</artifactId>
<!-- spring boot 1.x最後穩定版本 -->
<version>1.4.1.RELEASE</version>
<!-- 表示父模組pom的相對路徑,這裡沒有值 -->
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 測試 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!-- 只在test測試裡面執行 -->
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>spring-boot-hello-world</finalName>
<plugins>
<!-- jdk編譯外掛 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
啟動類Application
package com.jege.spring.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author JE哥
* @email [email protected]
* @description:spring boot 啟動類
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
控制器HelloWorldController
package com.jege.spring.boot.hello.world;
import java.util.Arrays;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author JE哥
* @email [email protected]
* @description:看看spring-boot的強大和方便
*/
@RestController
public class HelloWorldController {
@RequestMapping("/hello1")
public String hello1() {
return "Hello World";
}
@RequestMapping("/hello2")
public List<String> hello2() {
return Arrays.asList(new String[] { "A", "B", "C" });
}
}
測試類HelloWorldControllerTest
package com.jege.spring.boot.hello.world;
import static org.hamcrest.Matchers.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.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
/**
* @author JE哥
* @email [email protected]
* @description:以Mock方式測試Controller
*/
@SpringBootTest
public class HelloWorldControllerTest {
private MockMvc mockMvc;
@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build();
}
@Test
public void getHello() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/hello1").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andExpect(content().string(equalTo("Hello World")));
}
@Test
public void getHello2() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/hello2").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andExpect(content().string(equalTo("[\"A\",\"B\",\"C\"]")));
}
}
執行
執行HelloWorldControllerTest
以Mock方式測試Controller
原始碼地址
如果覺得我的文章或者程式碼對您有幫助,可以請我喝杯咖啡。
您的支援將鼓勵我繼續創作!謝謝!