Skip to content

Commit ea83d20

Browse files
committed
Spring Boot 2.x基础教程:构建RESTful API与单元测试
1 parent 23b766d commit ea83d20

File tree

8 files changed

+269
-0
lines changed

8 files changed

+269
-0
lines changed

chapter2-1/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/
27+
28+
### VS Code ###
29+
.vscode/

chapter2-1/pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.1.3.RELEASE</version>
10+
<relativePath/> <!-- lookup parent from repository -->
11+
</parent>
12+
13+
<groupId>com.didispace</groupId>
14+
<artifactId>chapter2-1</artifactId>
15+
<version>0.0.1-SNAPSHOT</version>
16+
<name>chapter2-1</name>
17+
<description>Demo project for Spring Boot</description>
18+
19+
<properties>
20+
<java.version>1.8</java.version>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.projectlombok</groupId>
31+
<artifactId>lombok</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
</dependencies>
40+
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-maven-plugin</artifactId>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
50+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.didispace.chapter21;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Chapter21Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Chapter21Application.class, args);
11+
}
12+
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.didispace.chapter21;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class User {
7+
8+
private Long id;
9+
private String name;
10+
private Integer age;
11+
12+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.didispace.chapter21;
2+
3+
import org.springframework.web.bind.annotation.*;
4+
5+
import java.util.*;
6+
7+
@RestController
8+
@RequestMapping(value = "/users") // 通过这里配置使下面的映射都在/users下
9+
public class UserController {
10+
11+
// 创建线程安全的Map,模拟users信息的存储
12+
static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());
13+
14+
/**
15+
* 处理"/users/"的GET请求,用来获取用户列表
16+
*
17+
* @return
18+
*/
19+
@GetMapping("/")
20+
public List<User> getUserList() {
21+
// 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
22+
List<User> r = new ArrayList<User>(users.values());
23+
return r;
24+
}
25+
26+
/**
27+
* 处理"/users/"的POST请求,用来创建User
28+
*
29+
* @param user
30+
* @return
31+
*/
32+
@PostMapping("/")
33+
public String postUser(@RequestBody User user) {
34+
// @RequestBody注解用来绑定通过http请求中application/json类型上传的数据
35+
users.put(user.getId(), user);
36+
return "success";
37+
}
38+
39+
/**
40+
* 处理"/users/{id}"的GET请求,用来获取url中id值的User信息
41+
*
42+
* @param id
43+
* @return
44+
*/
45+
@GetMapping("/{id}")
46+
public User getUser(@PathVariable Long id) {
47+
// url中的id可通过@PathVariable绑定到函数的参数中
48+
return users.get(id);
49+
}
50+
51+
/**
52+
* 处理"/users/{id}"的PUT请求,用来更新User信息
53+
*
54+
* @param id
55+
* @param user
56+
* @return
57+
*/
58+
@PutMapping("/{id}")
59+
public String putUser(@PathVariable Long id, @RequestBody User user) {
60+
User u = users.get(id);
61+
u.setName(user.getName());
62+
u.setAge(user.getAge());
63+
users.put(id, u);
64+
return "success";
65+
}
66+
67+
/**
68+
* 处理"/users/{id}"的DELETE请求,用来删除User
69+
*
70+
* @param id
71+
* @return
72+
*/
73+
@DeleteMapping("/{id}")
74+
public String deleteUser(@PathVariable Long id) {
75+
users.remove(id);
76+
return "success";
77+
}
78+
79+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.didispace.chapter21;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.http.MediaType;
8+
import org.springframework.test.context.junit4.SpringRunner;
9+
import org.springframework.test.web.servlet.MockMvc;
10+
import org.springframework.test.web.servlet.RequestBuilder;
11+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
12+
13+
import static org.hamcrest.Matchers.equalTo;
14+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
15+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
16+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
17+
18+
19+
@RunWith(SpringRunner.class)
20+
@SpringBootTest
21+
public class Chapter21ApplicationTests {
22+
23+
private MockMvc mvc;
24+
25+
@Before
26+
public void setUp() {
27+
mvc = MockMvcBuilders.standaloneSetup(new UserController()).build();
28+
}
29+
30+
@Test
31+
public void testUserController() throws Exception {
32+
// 测试UserController
33+
RequestBuilder request;
34+
35+
// 1、get查一下user列表,应该为空
36+
request = get("/users/");
37+
mvc.perform(request)
38+
.andExpect(status().isOk())
39+
.andExpect(content().string(equalTo("[]")));
40+
41+
// 2、post提交一个user
42+
request = post("/users/")
43+
.contentType(MediaType.APPLICATION_JSON)
44+
.content("{\"id\":1,\"name\":\"测试大师\",\"age\":20}");
45+
mvc.perform(request)
46+
.andExpect(content().string(equalTo("success")));
47+
48+
// 3、get获取user列表,应该有刚才插入的数据
49+
request = get("/users/");
50+
mvc.perform(request)
51+
.andExpect(status().isOk())
52+
.andExpect(content().string(equalTo("[{\"id\":1,\"name\":\"测试大师\",\"age\":20}]")));
53+
54+
// 4、put修改id为1的user
55+
request = put("/users/1")
56+
.contentType(MediaType.APPLICATION_JSON)
57+
.content("{\"name\":\"测试终极大师\",\"age\":30}");
58+
mvc.perform(request)
59+
.andExpect(content().string(equalTo("success")));
60+
61+
// 5、get一个id为1的user
62+
request = get("/users/1");
63+
mvc.perform(request)
64+
.andExpect(content().string(equalTo("{\"id\":1,\"name\":\"测试终极大师\",\"age\":30}")));
65+
66+
// 6、del删除id为1的user
67+
request = delete("/users/1");
68+
mvc.perform(request)
69+
.andExpect(content().string(equalTo("success")));
70+
71+
// 7、get查一下user列表,应该为空
72+
request = get("/users/");
73+
mvc.perform(request)
74+
.andExpect(status().isOk())
75+
.andExpect(content().string(equalTo("[]")));
76+
77+
}
78+
79+
}

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@
99
<description>全网Star最多的Spring Boot基础教程</description>
1010

1111
<modules>
12+
<!--快速入门-->
1213
<module>chapter1-1</module>
1314

15+
<!--Web开发-->
16+
<module>chapter2-1</module>
17+
18+
19+
1420
</modules>
1521

1622

0 commit comments

Comments
 (0)