1. 程式人生 > >spring-boot的helloWorld版本

spring-boot的helloWorld版本

open group 建議 ber schema snapshot exceptio ges hot

1.運行環境

開發工具:intellij idea

JDK版本:1.8

項目管理工具:Maven 3.2.5

2.Maven Plugin管理

pom.xml配置代碼:

技術分享圖片
 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3   <modelVersion
>4.0.0</modelVersion> 4 5 <groupId>spring-boot-helloWorld</groupId> 6 <artifactId>spring-boot-helloWorld</artifactId> 7 <version>1.0-SNAPSHOT</version> 8 9 <!-- Spring Boot 啟動父依賴 --> 10 <parent> 11 <groupId>org.springframework.boot</
groupId> 12 <artifactId>spring-boot-starter-parent</artifactId> 13 <version>1.3.3.RELEASE</version> 14 </parent> 15 16 <dependencies> 17 <!-- Spring Boot web依賴 --> 18 <dependency> 19 <groupId>org.springframework.boot</
groupId> 20 <artifactId>spring-boot-starter-web</artifactId> 21 </dependency> 22 <!-- Spring Boot test依賴 --> 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-test</artifactId> 26 <scope>test</scope> 27 </dependency> 28 </dependencies> 29 </project>
View Code

3.Application啟動類編寫

技術分享圖片
 1 package com.goku.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.web.servlet.ServletComponentScan;
 6 
 7 /**
 8  * Created by nbfujx on 2017/11/20.
 9  */
10 // Spring Boot 應用的標識
11 @SpringBootApplication
12 @ServletComponentScan
13 public class DemoApplication {
14 
15     public static void main(String[] args) {
16         // 程序啟動入口
17         // 啟動嵌入式的 Tomcat 並初始化 Spring 環境及其各 Spring 組件
18         SpringApplication.run(DemoApplication.class,args);
19     }
20 }
View Code

4.ExampleController控制器編寫

技術分享圖片
 1 package com.goku.demo.controller;
 2 
 3 import org.springframework.web.bind.annotation.PathVariable;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RestController;
 6 
 7 /**
 8  * Created by nbfujx on 2017-11-20.
 9  */
10 @RestController
11 public class ExampleController {
12 
13     @RequestMapping("/")
14     public String helloWorld()
15     {
16         return "helloWorld";
17     }
18 
19     @RequestMapping("/{str}")
20     public String helloWorld(@PathVariable  String str)
21     {
22         return "hello"+ str;
23     }
24 }
View Code

5.使用MockMvc對Controller進行測試

添加相關單元測試

技術分享圖片
 1 package test.com.goku.demo.controller;
 2 
 3 import com.goku.demo.DemoApplication;
 4 import com.goku.demo.controller.ExampleController;
 5 import org.junit.Before;
 6 import org.junit.Test;
 7 import org.junit.runner.RunWith;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.boot.test.context.SpringBootTest;
10 import org.springframework.http.MediaType;
11 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12 import org.springframework.test.context.web.WebAppConfiguration;
13 import org.springframework.test.web.servlet.MockMvc;
14 import org.springframework.test.web.servlet.RequestBuilder;
15 import org.springframework.test.web.servlet.ResultActions;
16 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
17 import org.springframework.web.context.WebApplicationContext;
18 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
19 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
20 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
21 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
22 
23 import static org.junit.Assert.*;
24 
25 /**
26  * Created by nbfujx on 2017-11-20.
27  */
28 @RunWith(SpringJUnit4ClassRunner.class)
29 @SpringBootTest(classes = DemoApplication.class)//這裏的Application是springboot的啟動類名。
30 @WebAppConfiguration
31 public class ExampleControllerTest {
32 
33     @Autowired
34     private WebApplicationContext context;
35     private MockMvc mvc;
36 
37     @Before
38     public void setUp() throws Exception {
39         mvc = MockMvcBuilders.webAppContextSetup(context).build();//建議使用這種
40     }
41 
42     @Test
43     public void helloWorld() throws Exception {
44         String responseString = mvc.perform(get("/")    //請求的url,請求的方法是get
45                 .contentType(MediaType.APPLICATION_JSON)  //數據的格式
46                 .param("pcode","root")         //添加參數
47         ).andExpect(status().isOk())    //返回的狀態是200
48                 .andDo(print())         //打印出請求和相應的內容
49                 .andReturn().getResponse().getContentAsString();   //將相應的數據轉換為字符串
50         System.out.println("--------返回的json = " + responseString);
51     }
52 
53     @Test
54     public void helloWorld1() throws Exception {
55         String responseString = mvc.perform(get("/str")    //請求的url,請求的方法是get
56                 .contentType(MediaType.APPLICATION_JSON)  //數據的格式
57                 .param("pcode","root")         //添加參數
58         ).andExpect(status().isOk())    //返回的狀態是200
59                 .andDo(print())         //打印出請求和相應的內容
60                 .andReturn().getResponse().getContentAsString();   //將相應的數據轉換為字符串
61         System.out.println("--------返回的json = " + responseString);
62     }
63 
64 }
View Code

6.在頁面上運行

http://localhost:8080/

技術分享圖片

http://localhost:8080/str

技術分享圖片

7.GITHUB地址

https://github.com/nbfujx/springBoot-learn-demo/tree/master/spring-boot-helloWorld

spring-boot的helloWorld版本