1. 程式人生 > >三、Springboot學習1-配置-2018-11-13

三、Springboot學習1-配置-2018-11-13

1. 專案建立

    1.1 訪問https://start.spring.io/,進行專案基礎配置,然後下載匯入到開發工具

     

    1.2 專案介面如圖:

 

2. 引入web依賴:

    1.1

<dependency>        

    <groupId>org.springframework.boot</groupId>        

    <artifactId>spring-boot-starter-web</artifactId>

</dependency>

    1.2

    

    pom.xml檔案中預設有兩個模組:

 spring-boot-starter :核心模組,包括自動配置支援、日誌和YAML;

 spring-boot-starter-test :測試模組,包括JUnit、Hamcrest、Mockito。

3.   編寫controller內容

@RestController
public class HelloWorldController {   
    @RequestMapping("/hello")    
    public String index() {
        return "Hello World";  

 } }

 4. 啟動專案,訪問http://localhost:8080/hello

 5. 進行單元測試

@RunWith(SpringRunner.class)
@WebAppConfiguration
public class SpringbootApplicationTests {

   private MockMvc mvc;

   @Before
   public void setUp() {
      mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
   }

   @Test
   public void contextLoads() throws Exception {
      mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andDo(MockMvcResultHandlers.print())
            .andReturn();
   }

}

    6. 載入熱部署

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <optional>true</optional>
</dependency>
<build>
   <plugins>
      <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
         <configuration>
            <fork>true</fork>
         </configuration>
      </plugin>
   </plugins>
</build>

  7. 上述涉及到的註解以及程式碼詳解:

      7.1 @RestController註解:

            @RestController = @Controller + @ResponseBody,自動將返回的物件實體轉為Json格式,無法返回jsp或html頁面, 如果需要返回到指定頁面,則需要用 @Controller配合檢視解析器InternalResourceViewResolver才行。  如果需要返回JSON,XML或自定義mediaType內容到頁面,則需要在對應的方法上加上@ResponseBody註解。

      7.2  @SpringBootApplication註解:

              @[email protected][email protected][email protected]

              @Configuration:是一個類級註釋,指示物件是一個bean定義的源。@Configuration 類通過 @bean 註解的公共方法宣告bean

               @Bean:是用來表示一個方法例項化,配置和初始化是由 Spring IoC 容器管理的一個新的物件。

               @EnableAutoConfiguration:啟用 Spring 應用程式上下文的自動配置,試圖猜測和配置您可能需要的bean。自動配置類通常採用基於你的 classpath 和已經定義的 beans 物件進行應用。被 @EnableAutoConfiguration 註解的類所在的包有特定的意義,並且作為預設配置使用。例如,當掃描 @Entity類的時候它將本使用。通常推薦將 @EnableAutoConfiguration 配置在 root 包下,這樣所有的子包、類都可以被查詢到。

                @ComponentScan:為 @Configuration註解的類配置元件掃描指令。同時提供與 Spring XML’s 元素並行的支援

      7.3 @RequestMapping:會將 HTTP 請求對映到 MVC 和 REST 控制器的處理方法上

      7.4 @RunWith就是一個執行器

            @RunWith(JUnit4.class)就是指用JUnit4來執行

            @RunWith(SpringJUnit4ClassRunner.class),讓測試運行於Spring測試環境

            @RunWith(Suite.class)的話就是一套測試集合

            @SpringApplicationConfiguration:匯入配置支援

            @WebAppConfiguration

         新版的Spring Boot取消了@SpringApplicationConfiguration這個註解,用@SpringBootTest

     7.5 單元測試學習部落格地址:https://www.cnblogs.com/lyy-2016/p/6122144.html

         7.5.1 通過MockMvcBuilders.standaloneSetup模擬一個Mvc測試環境,放置要測試的類,通過build方法得到Mockmvc

         7.5.2 寫測試類,執行Mockmvc的

                      perform方法,會自動執行springmvc流程,並對映到相應控住器。

                               MockMvcRequestBuilders.get("/user/1")構造一個請求

                      andExpect:新增ResultMatcher驗證規則,驗證控制器執行完成後結果是否正確;
                      andDo:新增ResultHandler結果處理器,比如除錯時列印結果到控制檯;
                      andReturn:最後返回相應的MvcResult;然後進行自定義驗證/進行下一步的非同步處理;

   整個測試過程非常有規律:
       1、準備測試環境
       2、通過MockMvc執行請求
       3.1、新增驗證斷言
       3.2、新增結果處理器
       3.3、得到MvcResult進行自定義斷言/進行下一步的非同步請求
       4、解除安裝測試環境