diff --git a/Chapter1/pom.xml b/Chapter1/pom.xml new file mode 100755 index 0000000..2f235cc --- /dev/null +++ b/Chapter1/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + + com.didispace + demo + 1.0.0 + jar + + demo + Spring Boot Web project + + + org.springframework.boot + spring-boot-starter-parent + 1.3.2.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + + + diff --git a/Chapter1/src/main/java/com/didispace/Application.java b/Chapter1/src/main/java/com/didispace/Application.java new file mode 100755 index 0000000..f7a9e16 --- /dev/null +++ b/Chapter1/src/main/java/com/didispace/Application.java @@ -0,0 +1,22 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + + SpringApplication.run(Application.class, args); + + } + +} diff --git a/Chapter1/src/main/java/com/didispace/web/HelloController.java b/Chapter1/src/main/java/com/didispace/web/HelloController.java new file mode 100755 index 0000000..66b3ea7 --- /dev/null +++ b/Chapter1/src/main/java/com/didispace/web/HelloController.java @@ -0,0 +1,29 @@ +package com.didispace.web; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@Controller +public class HelloController { + + @RequestMapping("/hello") + public String hello() { + return "Hello World"; + } + + @RequestMapping("/") + public String index(ModelMap map) { + map.addAttribute("host", "http://blog.didispace.com"); + return "index"; + } + +} \ No newline at end of file diff --git a/Chapter1/src/main/resources/application.properties b/Chapter1/src/main/resources/application.properties new file mode 100755 index 0000000..e69de29 diff --git a/Chapter1/src/main/resources/templates/index.html b/Chapter1/src/main/resources/templates/index.html new file mode 100755 index 0000000..32cbfe8 --- /dev/null +++ b/Chapter1/src/main/resources/templates/index.html @@ -0,0 +1,10 @@ + + + + + + + +

Hello World

+ + \ No newline at end of file diff --git a/Chapter1/src/test/java/com/didispace/ApplicationTests.java b/Chapter1/src/test/java/com/didispace/ApplicationTests.java new file mode 100755 index 0000000..c5b2e60 --- /dev/null +++ b/Chapter1/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,48 @@ +package com.didispace; + +import com.didispace.web.HelloController; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockServletContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +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; + + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = MockServletContext.class) +@WebAppConfiguration +public class ApplicationTests { + + private MockMvc mvc; + + @Before + public void setUp() throws Exception { + 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"))); + } + +} diff --git a/Chapter3-1-1/pom.xml b/Chapter3-1-1/pom.xml new file mode 100755 index 0000000..9b5dac7 --- /dev/null +++ b/Chapter3-1-1/pom.xml @@ -0,0 +1,56 @@ + + + 4.0.0 + + com.didispace + Chapter1 + 1.0.0 + jar + + Chapter1 + The first Spring Boot project + + + org.springframework.boot + spring-boot-starter-parent + 1.3.2.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/Chapter3-1-1/src/main/java/com/didispace/Application.java b/Chapter3-1-1/src/main/java/com/didispace/Application.java new file mode 100755 index 0000000..f7a9e16 --- /dev/null +++ b/Chapter3-1-1/src/main/java/com/didispace/Application.java @@ -0,0 +1,22 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + + SpringApplication.run(Application.class, args); + + } + +} diff --git a/Chapter3-1-1/src/main/java/com/didispace/domain/User.java b/Chapter3-1-1/src/main/java/com/didispace/domain/User.java new file mode 100755 index 0000000..208cfe3 --- /dev/null +++ b/Chapter3-1-1/src/main/java/com/didispace/domain/User.java @@ -0,0 +1,41 @@ +package com.didispace.domain; + + + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +public class User { + + private Long id; + private String name; + private Integer age; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } +} diff --git a/Chapter3-1-1/src/main/java/com/didispace/web/HelloController.java b/Chapter3-1-1/src/main/java/com/didispace/web/HelloController.java new file mode 100755 index 0000000..58c767e --- /dev/null +++ b/Chapter3-1-1/src/main/java/com/didispace/web/HelloController.java @@ -0,0 +1,21 @@ +package com.didispace.web; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@RestController +public class HelloController { + + @RequestMapping("/hello") + public String index() { + return "Hello World"; + } + +} \ No newline at end of file diff --git a/Chapter3-1-1/src/main/java/com/didispace/web/UserController.java b/Chapter3-1-1/src/main/java/com/didispace/web/UserController.java new file mode 100755 index 0000000..f5c7ee1 --- /dev/null +++ b/Chapter3-1-1/src/main/java/com/didispace/web/UserController.java @@ -0,0 +1,62 @@ +package com.didispace.web; + +import com.didispace.domain.User; + +import java.util.*; + +import org.springframework.web.bind.annotation.*; + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@RestController +@RequestMapping(value="/users") // 通过这里配置使下面的映射都在/users下,可去除 +public class UserController { + + static Map users = Collections.synchronizedMap(new HashMap()); + + @RequestMapping(value="/", method=RequestMethod.GET) + public List getUserList() { + // 处理"/users/"的GET请求,用来获取用户列表 + // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递 + List r = new ArrayList(users.values()); + return r; + } + + @RequestMapping(value="/", method=RequestMethod.POST) + public String postUser(@ModelAttribute User user) { + // 处理"/users/"的POST请求,用来创建User + // 除了@ModelAttribute绑定参数之外,还可以通过@RequestParam从页面中传递参数 + users.put(user.getId(), user); + return "success"; + } + + @RequestMapping(value="/{id}", method=RequestMethod.GET) + public User getUser(@PathVariable Long id) { + // 处理"/users/{id}"的GET请求,用来获取url中id值的User信息 + // url中的id可通过@PathVariable绑定到函数的参数中 + return users.get(id); + } + + @RequestMapping(value="/{id}", method=RequestMethod.PUT) + public String putUser(@PathVariable Long id, @ModelAttribute User user) { + // 处理"/users/{id}"的PUT请求,用来更新User信息 + User u = users.get(id); + u.setName(user.getName()); + u.setAge(user.getAge()); + users.put(id, u); + return "success"; + } + + @RequestMapping(value="/{id}", method=RequestMethod.DELETE) + public String deleteUser(@PathVariable Long id) { + // 处理"/users/{id}"的DELETE请求,用来删除User + users.remove(id); + return "success"; + } + +} \ No newline at end of file diff --git a/Chapter3-1-1/src/main/resources/application.properties b/Chapter3-1-1/src/main/resources/application.properties new file mode 100755 index 0000000..e69de29 diff --git a/Chapter3-1-1/src/test/java/com/didispace/ApplicationTests.java b/Chapter3-1-1/src/test/java/com/didispace/ApplicationTests.java new file mode 100755 index 0000000..1160986 --- /dev/null +++ b/Chapter3-1-1/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,104 @@ +package com.didispace; + +import com.didispace.domain.User; +import com.didispace.web.HelloController; +import com.didispace.web.UserController; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockServletContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.RequestBuilder; +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.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = MockServletContext.class) +@WebAppConfiguration +public class ApplicationTests { + + private MockMvc mvc; + + @Before + public void setUp() throws Exception { + mvc = MockMvcBuilders.standaloneSetup( + new HelloController(), + new UserController()).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"))); + } + + @Test + public void testUserController() throws Exception { +// 测试UserController + RequestBuilder request = null; + + // 1、get查一下user列表,应该为空 + request = get("/users/"); + mvc.perform(request) + .andExpect(status().isOk()) + .andExpect(content().string(equalTo("[]"))); + + // 2、post提交一个user + request = post("/users/") + .param("id", "1") + .param("name", "测试大师") + .param("age", "20"); + mvc.perform(request) +// .andDo(MockMvcResultHandlers.print()) + .andExpect(content().string(equalTo("success"))); + + // 3、get获取user列表,应该有刚才插入的数据 + request = get("/users/"); + mvc.perform(request) + .andExpect(status().isOk()) + .andExpect(content().string(equalTo("[{\"id\":1,\"name\":\"测试大师\",\"age\":20}]"))); + + // 4、put修改id为1的user + request = put("/users/1") + .param("name", "测试终极大师") + .param("age", "30"); + mvc.perform(request) + .andExpect(content().string(equalTo("success"))); + + // 5、get一个id为1的user + request = get("/users/1"); + mvc.perform(request) + .andExpect(content().string(equalTo("{\"id\":1,\"name\":\"测试终极大师\",\"age\":30}"))); + + // 6、del删除id为1的user + request = delete("/users/1"); + mvc.perform(request) + .andExpect(content().string(equalTo("success"))); + + // 7、get查一下user列表,应该为空 + request = get("/users/"); + mvc.perform(request) + .andExpect(status().isOk()) + .andExpect(content().string(equalTo("[]"))); + + } + +} diff --git a/Chapter3-1-2/pom.xml b/Chapter3-1-2/pom.xml new file mode 100755 index 0000000..cc4e377 --- /dev/null +++ b/Chapter3-1-2/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + + com.didispace + Chapter1 + 1.0.0 + jar + + Chapter1 + The first Spring Boot project + + + org.springframework.boot + spring-boot-starter-parent + 1.3.2.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/Chapter3-1-2/src/main/java/com/didispace/Chapter1Application.java b/Chapter3-1-2/src/main/java/com/didispace/Chapter1Application.java new file mode 100755 index 0000000..6823281 --- /dev/null +++ b/Chapter3-1-2/src/main/java/com/didispace/Chapter1Application.java @@ -0,0 +1,13 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Chapter1Application { + + public static void main(String[] args) { + SpringApplication.run(Chapter1Application.class, args); + } + +} diff --git a/Chapter3-1-2/src/main/java/com/didispace/web/HelloController.java b/Chapter3-1-2/src/main/java/com/didispace/web/HelloController.java new file mode 100755 index 0000000..9455485 --- /dev/null +++ b/Chapter3-1-2/src/main/java/com/didispace/web/HelloController.java @@ -0,0 +1,14 @@ +package com.didispace.web; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HelloController { + + @RequestMapping("/hello") + public String index() { + return "Hello World"; + } + +} \ No newline at end of file diff --git a/Chapter3-1-2/src/main/resources/application.properties b/Chapter3-1-2/src/main/resources/application.properties new file mode 100755 index 0000000..e69de29 diff --git a/Chapter3-1-2/src/test/java/com/didispace/Chapter1ApplicationTests.java b/Chapter3-1-2/src/test/java/com/didispace/Chapter1ApplicationTests.java new file mode 100755 index 0000000..0b5bfb0 --- /dev/null +++ b/Chapter3-1-2/src/test/java/com/didispace/Chapter1ApplicationTests.java @@ -0,0 +1,40 @@ +package com.didispace; + +import com.didispace.web.HelloController; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockServletContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +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(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = MockServletContext.class) +@WebAppConfiguration +public class Chapter1ApplicationTests { + + private MockMvc mvc; + + @Before + public void setUp() throws Exception { + 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"))); + } + +}