Skip to content

Commit c52d1b8

Browse files
committed
快速开始完结
1 parent 9351826 commit c52d1b8

File tree

4 files changed

+208
-2
lines changed

4 files changed

+208
-2
lines changed

spring-boot-quick-start/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<description>spring-boot-quick-start</description>
1616

1717
<properties>
18-
<java.version>1.8</java.version>
18+
<java.version>11</java.version>
1919
</properties>
2020

2121
<dependencies>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.springboot.springbootquickstart.controller;
2+
3+
import com.springboot.springbootquickstart.model.UserModel;
4+
import org.springframework.web.bind.annotation.*;
5+
6+
import java.util.*;
7+
import java.util.concurrent.ConcurrentHashMap;
8+
9+
/**
10+
* Created with IntelliJ IDEA.
11+
*
12+
* @Date: 2019/9/11
13+
* @Time: 22:15
14+
15+
* Description:
16+
*/
17+
@RestController
18+
public class UserController {
19+
20+
// 创建线程安全的Map,用作数据存储
21+
static Map<Long, UserModel> users = new ConcurrentHashMap<Long, UserModel>();
22+
23+
/**
24+
* 查询用户列表
25+
* @return
26+
*/
27+
@GetMapping("/")
28+
public List<UserModel> getUserList() {
29+
List<UserModel> list = new ArrayList<UserModel>(users.values());
30+
return list;
31+
}
32+
33+
/**
34+
* 创建User
35+
* @param userModel
36+
* @return
37+
*/
38+
@PostMapping("/")
39+
public UserModel postUser(@ModelAttribute UserModel userModel) {
40+
users.put(userModel.getId(), userModel);
41+
return users.get(userModel.getId());
42+
}
43+
44+
/**
45+
* {id} 根据 url 中的 id 获取 user 信息
46+
* url中的id可通过@PathVariable绑定到函数的参数中
47+
* @param id
48+
* @return
49+
*/
50+
@GetMapping("/{id}")
51+
public UserModel getUser(@PathVariable Long id) {
52+
return users.get(id);
53+
}
54+
55+
/**
56+
* 根据 id 更新用户信息
57+
* @param id
58+
* @param userModel
59+
* @return
60+
*/
61+
@PutMapping("/{id}")
62+
public UserModel putUser(@PathVariable Long id, @ModelAttribute UserModel userModel) {
63+
UserModel u = users.get(id);
64+
u.setName(userModel.getName());
65+
u.setAge(userModel.getAge());
66+
users.put(id, u);
67+
return users.get(userModel.getId());
68+
}
69+
70+
/**
71+
* 根据 id 删除用户信息
72+
* @param id
73+
* @return
74+
*/
75+
@DeleteMapping("/{id}")
76+
public String deleteUser(@PathVariable Long id) {
77+
users.remove(id);
78+
return "success";
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.springboot.springbootquickstart.model;
2+
3+
/**
4+
* Created with IntelliJ IDEA.
5+
*
6+
* @Date: 2019/9/11
7+
* @Time: 22:17
8+
9+
* Description:
10+
*/
11+
public class UserModel {
12+
private Long id;
13+
private String name;
14+
private int age;
15+
16+
public Long getId() {
17+
return id;
18+
}
19+
20+
public void setId(Long id) {
21+
this.id = id;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public void setName(String name) {
29+
this.name = name;
30+
}
31+
32+
public int getAge() {
33+
return age;
34+
}
35+
36+
public void setAge(int age) {
37+
this.age = age;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,103 @@
11
package com.springboot.springbootquickstart;
22

3+
import com.springboot.springbootquickstart.controller.UserController;
4+
import org.junit.Before;
35
import org.junit.Test;
46
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
58
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.http.MediaType;
610
import org.springframework.test.context.junit4.SpringRunner;
11+
import org.springframework.test.web.servlet.MockMvc;
12+
import org.springframework.test.web.servlet.MvcResult;
13+
import org.springframework.test.web.servlet.RequestBuilder;
14+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
15+
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
16+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
17+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
18+
import org.springframework.web.context.WebApplicationContext;
719

820
@RunWith(SpringRunner.class)
921
@SpringBootTest
1022
public class SpringBootQuickStartApplicationTests {
1123

24+
private MockMvc mvc;
25+
26+
@Before
27+
public void setUp() throws Exception {
28+
mvc = MockMvcBuilders.standaloneSetup(new UserController()).build();
29+
}
30+
1231
@Test
13-
public void contextLoads() {
32+
public void contextLoads() throws Exception {
33+
RequestBuilder request = null;
34+
35+
// 1、get查一下user列表,应该为空
36+
request = MockMvcRequestBuilders.get("/")
37+
.contentType(MediaType.APPLICATION_JSON);
38+
mvc.perform(request)
39+
.andExpect(MockMvcResultMatchers.status().isOk())
40+
.andDo(MockMvcResultHandlers.print())
41+
.andReturn();
42+
43+
// 2、post提交一个user
44+
request = MockMvcRequestBuilders.post("/")
45+
.param("id", "1")
46+
.param("name", "Spring Boot")
47+
.param("age", "18")
48+
.contentType(MediaType.APPLICATION_JSON);
49+
mvc.perform(request)
50+
.andExpect(MockMvcResultMatchers.status().isOk())
51+
.andDo(MockMvcResultHandlers.print())
52+
.andReturn();
53+
54+
55+
// 3、get获取user列表,应该有刚才插入的数据
56+
request = MockMvcRequestBuilders.get("/")
57+
.contentType(MediaType.APPLICATION_JSON);
58+
mvc.perform(request)
59+
.andExpect(MockMvcResultMatchers.status().isOk())
60+
.andDo(MockMvcResultHandlers.print())
61+
.andReturn();
62+
63+
// 4、put修改id为1的user
64+
request = MockMvcRequestBuilders.put("/1")
65+
.param("name", "Spring Boot Test")
66+
.contentType(MediaType.APPLICATION_JSON);
67+
68+
mvc.perform(request)
69+
.andExpect(MockMvcResultMatchers.status().isOk())
70+
.andDo(MockMvcResultHandlers.print())
71+
.andReturn();
72+
73+
// 5、get一个id为1的user
74+
request = MockMvcRequestBuilders.get("/1")
75+
.contentType(MediaType.APPLICATION_JSON);
76+
77+
mvc.perform(request)
78+
.andExpect(MockMvcResultMatchers.status().isOk())
79+
.andDo(MockMvcResultHandlers.print())
80+
.andReturn();
81+
82+
// 6、del删除id为1的user
83+
request = MockMvcRequestBuilders.delete("/1")
84+
.contentType(MediaType.APPLICATION_JSON);
85+
86+
mvc.perform(request)
87+
.andExpect(MockMvcResultMatchers.status().isOk())
88+
.andDo(MockMvcResultHandlers.print())
89+
.andReturn();
90+
91+
// 7、get查一下user列表,应该为空
92+
93+
request = MockMvcRequestBuilders.get("/")
94+
.contentType(MediaType.APPLICATION_JSON);
95+
96+
mvc.perform(request)
97+
.andExpect(MockMvcResultMatchers.status().isOk())
98+
.andDo(MockMvcResultHandlers.print())
99+
.andReturn();
100+
14101
}
15102

16103
}

0 commit comments

Comments
 (0)