每个PHP程序员基本都有一段学JAVA的艰辛之路,无论是正在学JAVA仍是已经放弃学JAVA的猿/媛,都被JAVA折磨过,一样,我也正在被折磨。。。java
用该系列文章记录我被折磨后的成果。mysql
我要分享的学习方法很简单,一个字:干git
很简单粗暴的方式,其实学习最好的方式就是输出,因此我会常常写博文,分享我学到的东西,只要把学习到的东西输出出来,才会深入。程序员
spring web starter
依赖:next
等待加载依赖完成出现该页面证实依赖加载完成。github
SpringBootStudyDemo1Application
文件输入以下代码:package cn.sockstack.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication //使用restful风格的controller @RestController public class SpringBootStudyDemo1Application { public static void main(String[] args) { SpringApplication.run(SpringBootStudyDemo1Application.class, args); } //添加'/'路由,输出hello SockStack! @GetMapping("/") public String hello() { return "hello SockStack!"; } }
spring-boot-study-demo1\src\test\java\cn\sockstack\demo\SpringBootStudyDemo1ApplicationTests.java
添加以下代码:package cn.sockstack.demo; import org.junit.Assert; 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.context.SpringBootTest; 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.MvcResult; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @RunWith(SpringRunner.class) @SpringBootTest public class SpringBootStudyDemo1ApplicationTests { @Autowired private WebApplicationContext webApplicationContext; private MockMvc mockMvc; @Before public void buildMockMvc() { mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test public void testHelloController() throws Exception { MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.TEXT_HTML_VALUE)).andReturn(); int status = mvcResult.getResponse().getStatus(); String contentAsString = mvcResult.getResponse().getContentAsString(); Assert.assertEquals(200, status); Assert.assertEquals("hello SockStack!", contentAsString); } }
启动测试,测试结果:web
测试经过,一样也能够在浏览器打开127.0.0.1:8080
,查看结果spring
更多精彩文章,请关注个人博客 SOCKSTACK,分享个人工做经验。