跳到主要內容

Spring boot v1.5 (一) introduce


微服務(microservice)當道的時代,快速已經成為王道的代名詞,然而實現他概念的架構不得不提到Spring bootSpring Cloud,當然還有DevOps、Container(Moby(Docker))等等(本系列會帶到這些觀念,但是會另外在其他章節來說明,我認為的微服務是怎樣的東西),所以本系列看標題也可以知道都是Spring boot的介紹跟我遇到的一些問題及想法,希望看的人可以快速進入Spring boot,開發真的不難,難的是改變。

本系列文章大致分為
  1. 介紹說明本次學習目的及學習預計完成時間
    • 說明學習目的
    • 預計完成時數
    • 影片
  2. 程式碼及設定檔相關分析說明
    • pom.xml說明
    • .properties or yml設定檔說明
    • 程式碼說明
  3. 其他
    • 遭遇到的問題
    • 參考連結
    • Code Example Github連結


學習目的:快速完成一隻Spring boot程式,並加入改 listener port及監控機制。
學習時數:1.5 hr
教學影片:



pom.xml 說明


spring-boot-starter-web:配置 Web Project所需的函式庫
spring-boot-starter-test:配置 unit or mock test 所需的函式庫
spring-boot-starter-actuator:配置監控spring boot所需的函式庫,後續spring cloud會使用到,所以一開就導入

程式碼說明
  • Example1.java

package louisz.springboot.introduce;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Louisz Spring boot introduce ex.1
 *
 */

@SpringBootApplication
public class Example1 {
 /**
  * 程式執行起點
  * 
  * @param args
  */
 public static void main(String[] args) {
  SpringApplication.run(Example1.class, args);

 }

}


  • Ex1Controller.java

package louisz.springboot.introduce;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Louisz Spring boot introduce ex.1
 *
 */

@SpringBootApplication
public class Example1 {
 /**
  * 程式執行起點
  * 
  * @param args
  */
 public static void main(String[] args) {
  SpringApplication.run(Example1.class, args);

 }

}
執行時請加上-Dserver.port=8090,就可以啟動時選擇要Listener Port
測試以下幾個網址是否正常
http://localhost:8090/
http://localhost:8090/health
http://localhost:8090/info

Github code(louisz.springboot.example1)

其他:MockTest
TestEx1Controller.java
package louisz.springboot.introduce;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
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 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;

@RunWith(SpringRunner.class)
@WebMvcTest(Ex1Controller.class)
public class TestEx1Controller {
 @Autowired
    private MockMvc mvc;
 
    @MockBean
    private Ex1Controller service;
    
    @Before
    public void setUp() throws Exception{
     mvc = MockMvcBuilders.standaloneSetup(new Ex1Controller()).build();
    }
    
    @Test
    public void testHello() throws Exception{
     mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
 .andExpect(status().isOk())
 .andExpect(content().string(equalTo("This is Louisz Spring Boot!!")));
    }
    
}


留言