部分參考於《深刻實踐Spring Boot》、《Spring實戰 第四版》與程序猿DD的有關博客。java
參考(嘟嘟獨立博客):http://tengj.top/2017/02/26/springboot1/web
搭建項目:spring
建立Spring Boot操做步驟以下:
1.在File菜單裏面選擇 New > Project,而後選擇Spring Initializr,接着以下圖一步步操做便可。json
根據上面的操做已經初始化了一個Spring Boot的框架了,項目結構以下:
如你所見,項目裏面基本沒有代碼,除了幾個空目錄外,還包含以下幾樣東西。springboot
------------------------------------------------------------------------------------------分割線------------------------------------------------------------------------------------------------------------------mvc
spring-boot-starter
:核心模塊,包括了自動配置支持、日誌和YAMLspring-boot-starter-test
:測試模塊,包括JUnit、Hamcrest、Mockitospring-boot-starter-web
:Web模塊一個 Spring Boot 案例應該包括四個部分(主加載類、邏輯實現類、單元測試類、以及資源配置文件)。app
1. 資源配置文件:這個文件主要記錄了框架下各類設置;前面,咱們提到過 Spring Boot 提供 Spring 的默認設置,因此一開始並不須要對這個文件作任何修改,讓框架內嵌的Web容器加載該文件便可。* 注意:命名爲application.properties
*,而且默認端口爲8080。框架
2. 主加載類:Spring Boot 框架下,最重要的一個類,也是啓動整個框架的入口。通常有兩種代碼模板,好像也沒有什麼區別。這裏先寫一種:spring-boot
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
3. 邏輯實現類:就是咱們提供的服務接口,通常就是咱們的Controller層。這裏實現一個簡單的」hello world!」的Controller,便於測試。 啓動項目後,訪問 http://localhost:8080/hello 來訪問這個控制器。單元測試
@RestController public class HelloController { @RequestMapping("/hello") public String index(){ return "hello world!"; } }
4. 單元測試類:顧名思義,就是一個用來測試咱們的邏輯實現類的類。
這裏使用 JUnit 模擬一個 http 請求來測試咱們的 HelloController。
同時,這裏涉及到Spring AOP
的@Before
,有興趣的也能夠去查看一下。
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = MockServletContext.class) @WebAppConfiguration //測試環境使用,用來表示測試環境使用的ApplicationContext將是WebApplicationContext類型的 public class ApplicationTest { private MockMvc mvc; @Before public void setUp() throws Exception{ //經過MockMvcBuilders.xxxSetup().build()建立一個MockMvc進行測試; mvc = MockMvcBuilders.standaloneSetup(new HelloController()).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!"))) .andDo(MockMvcResultHandlers.print()) .andReturn(); } /** * 一、mockMvc.perform執行一個請求。 * 二、MockMvcRequestBuilders.get("XXX")構造一個請求。 * 三、ResultActions.andExpect添加執行完成後的斷言。 * 四、ResultActions.andDo添加一個結果處理器,表示要對結果作點什麼事情 * 好比此處使用MockMvcResultHandlers.print()輸出整個響應結果信息。 * 五、ResultActions.andReturn表示執行完成後返回相應的結果。 */ }
最後附上, http 請求響應後的報文。
MockHttpServletRequest: HTTP Method = GET Request URI = /hello Parameters = {} Headers = {Accept=[application/json]} Handler: Type = qg.fangrui.boot.web.HelloController Method = public java.lang.String qg.fangrui.boot.web.HelloController.index() Async: Async started = false Async result = null Resolved Exception: Type = null ModelAndView: View name = null View = null Model = null FlashMap: Attributes = null MockHttpServletResponse: Status = 200 Error message = null Headers = {Content-Type=[application/json;charset=ISO-8859-1], Content-Length=[12]} Content type = application/json;charset=ISO-8859-1 Body = hello world! Forwarded URL = null Redirected URL = null Cookies = []