Skip to content

Commit e1aa87b

Browse files
author
YunaiV
committed
增加 springmvc 示例
1 parent d14162e commit e1aa87b

File tree

24 files changed

+147
-51
lines changed

24 files changed

+147
-51
lines changed

lab-23/lab-springmvc-23-01/src/main/java/cn/iocoder/springboot/lab21/springmvc/Application.java lab-23/lab-springmvc-23-01/src/main/java/cn/iocoder/springboot/lab23/springmvc/Application.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cn.iocoder.springboot.lab21.springmvc;
1+
package cn.iocoder.springboot.lab23.springmvc;
22

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

lab-23/lab-springmvc-23-01/src/main/java/cn/iocoder/springboot/lab21/springmvc/controller/UserController.java lab-23/lab-springmvc-23-01/src/main/java/cn/iocoder/springboot/lab23/springmvc/controller/UserController.java

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
package cn.iocoder.springboot.lab21.springmvc.controller;
1+
package cn.iocoder.springboot.lab23.springmvc.controller;
22

3-
import cn.iocoder.springboot.lab21.springmvc.dto.UserAddDTO;
4-
import cn.iocoder.springboot.lab21.springmvc.dto.UserUpdateDTO;
5-
import cn.iocoder.springboot.lab21.springmvc.vo.UserVO;
3+
import cn.iocoder.springboot.lab23.springmvc.dto.UserAddDTO;
4+
import cn.iocoder.springboot.lab23.springmvc.dto.UserUpdateDTO;
5+
import cn.iocoder.springboot.lab23.springmvc.service.UserService;
6+
import cn.iocoder.springboot.lab23.springmvc.vo.UserVO;
7+
import org.springframework.beans.factory.annotation.Autowired;
68
import org.springframework.web.bind.annotation.*;
79

810
import java.util.ArrayList;
@@ -15,6 +17,9 @@
1517
@RequestMapping("/users")
1618
public class UserController {
1719

20+
@Autowired
21+
private UserService userService;
22+
1823
/**
1924
* 查询用户列表
2025
*
@@ -43,6 +48,17 @@ public UserVO get(@PathVariable("id") Integer id) {
4348
return new UserVO().setId(id).setUsername("username:" + id);
4449
}
4550

51+
/**
52+
* 获得指定用户编号的用户
53+
*
54+
* @param id 用户编号
55+
* @return 用户
56+
*/
57+
@GetMapping("/v2/{id}")
58+
public UserVO get2(@PathVariable("id") Integer id) {
59+
return userService.get(id);
60+
}
61+
4662
/**
4763
* 添加用户
4864
*

lab-23/lab-springmvc-23-01/src/main/java/cn/iocoder/springboot/lab21/springmvc/controller/UserController2.java lab-23/lab-springmvc-23-01/src/main/java/cn/iocoder/springboot/lab23/springmvc/controller/UserController2.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package cn.iocoder.springboot.lab21.springmvc.controller;
1+
package cn.iocoder.springboot.lab23.springmvc.controller;
22

3-
import cn.iocoder.springboot.lab21.springmvc.dto.UserAddDTO;
4-
import cn.iocoder.springboot.lab21.springmvc.dto.UserUpdateDTO;
5-
import cn.iocoder.springboot.lab21.springmvc.vo.UserVO;
3+
import cn.iocoder.springboot.lab23.springmvc.dto.UserAddDTO;
4+
import cn.iocoder.springboot.lab23.springmvc.dto.UserUpdateDTO;
5+
import cn.iocoder.springboot.lab23.springmvc.vo.UserVO;
66
import org.springframework.web.bind.annotation.*;
77

88
import java.util.ArrayList;

lab-23/lab-springmvc-23-01/src/main/java/cn/iocoder/springboot/lab21/springmvc/dto/UserAddDTO.java lab-23/lab-springmvc-23-01/src/main/java/cn/iocoder/springboot/lab23/springmvc/dto/UserAddDTO.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cn.iocoder.springboot.lab21.springmvc.dto;
1+
package cn.iocoder.springboot.lab23.springmvc.dto;
22

33
/**
44
* 用户添加 DTO

lab-23/lab-springmvc-23-01/src/main/java/cn/iocoder/springboot/lab21/springmvc/dto/UserUpdateDTO.java lab-23/lab-springmvc-23-01/src/main/java/cn/iocoder/springboot/lab23/springmvc/dto/UserUpdateDTO.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cn.iocoder.springboot.lab21.springmvc.dto;
1+
package cn.iocoder.springboot.lab23.springmvc.dto;
22

33
public class UserUpdateDTO {
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springboot.lab23.springmvc.service;
2+
3+
import cn.iocoder.springboot.lab23.springmvc.vo.UserVO;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
public class UserService {
8+
9+
public UserVO get(Integer id) {
10+
return new UserVO().setId(id).setUsername("test");
11+
}
12+
13+
}

lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab21/springmvc/vo/UserVO.java lab-23/lab-springmvc-23-01/src/main/java/cn/iocoder/springboot/lab23/springmvc/vo/UserVO.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cn.iocoder.springboot.lab21.springmvc.vo;
1+
package cn.iocoder.springboot.lab23.springmvc.vo;
22

33
/**
44
* 用户 VO

lab-23/lab-springmvc-23-01/src/test/java/cn/iocoder/springboot/lab21/springmvc/package-info.java

-1
This file was deleted.

lab-23/lab-springmvc-23-01/src/test/java/cn/iocoder/springboot/lab21/springmvc/controller/UserControllerTest.java lab-23/lab-springmvc-23-01/src/test/java/cn/iocoder/springboot/lab23/springmvc/controller/UserControllerTest.java

+29-13
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
1-
package cn.iocoder.springboot.lab21.springmvc.controller;
1+
package cn.iocoder.springboot.lab23.springmvc.controller;
22

3-
import cn.iocoder.springboot.lab21.springmvc.Application;
4-
import org.junit.Before;
3+
import cn.iocoder.springboot.lab23.springmvc.Application;
54
import org.junit.Test;
65
import org.junit.runner.RunWith;
76
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
88
import org.springframework.boot.test.context.SpringBootTest;
99
import org.springframework.test.context.junit4.SpringRunner;
1010
import org.springframework.test.web.servlet.MockMvc;
11+
import org.springframework.test.web.servlet.MvcResult;
1112
import org.springframework.test.web.servlet.ResultActions;
1213
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
14+
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
1315
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
14-
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
1516

17+
/**
18+
* UserController 集成测试
19+
*/
1620
@RunWith(SpringRunner.class)
1721
@SpringBootTest(classes = Application.class)
22+
@AutoConfigureMockMvc
1823
public class UserControllerTest {
1924

20-
private MockMvc mvc;
21-
2225
@Autowired
23-
private UserController userController;
24-
25-
@Before
26-
public void setUp() {
27-
mvc = MockMvcBuilders.standaloneSetup(userController).build();
28-
// mvc = MockMvcBuilders.standaloneSetup(new UserController()).build();
29-
}
26+
private MockMvc mvc;
3027

3128
@Test
3229
public void testList() throws Exception {
@@ -62,6 +59,25 @@ public void testGet() throws Exception {
6259
"}")); // 响应结果
6360
}
6461

62+
@Test
63+
public void testGet2() throws Exception {
64+
// 获得指定用户编号的用户
65+
ResultActions resultActions = mvc.perform(MockMvcRequestBuilders.get("/users/1"));
66+
// 校验结果
67+
resultActions.andExpect(MockMvcResultMatchers.status().isOk()); // 响应状态码 200
68+
resultActions.andExpect(MockMvcResultMatchers.content().json("{\n" +
69+
"\"id\": 1,\n" +
70+
"\"username\": \"username:1\"\n" +
71+
"}")); // 响应结果
72+
73+
// 打印结果
74+
resultActions.andDo(MockMvcResultHandlers.print());
75+
76+
// 获得 MvcResult ,后续执行各种自定义逻辑
77+
MvcResult mvcResult = resultActions.andReturn();
78+
System.out.println("拦截器数量:" + mvcResult.getInterceptors().length);
79+
}
80+
6581
@Test
6682
public void testAdd() throws Exception {
6783
// 获得指定用户编号的用户
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cn.iocoder.springboot.lab23.springmvc.controller;
2+
3+
import cn.iocoder.springboot.lab23.springmvc.service.UserService;
4+
import cn.iocoder.springboot.lab23.springmvc.vo.UserVO;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.mockito.Mockito;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
10+
import org.springframework.boot.test.mock.mockito.MockBean;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
import org.springframework.test.web.servlet.MockMvc;
13+
import org.springframework.test.web.servlet.ResultActions;
14+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
15+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
16+
17+
/**
18+
* UserController 单元测试
19+
*
20+
* 参考 https://spring.io/guides/gs/testing-web/ 文章
21+
*/
22+
@RunWith(SpringRunner.class)
23+
@WebMvcTest(UserController.class)
24+
public class UserControllerTest2 {
25+
26+
@Autowired
27+
private MockMvc mvc;
28+
29+
@MockBean
30+
private UserService userService;
31+
32+
@Test
33+
public void testGet2() throws Exception {
34+
// Mock UserService 的 get 方法
35+
System.out.println("before mock:" + userService.get(1));
36+
Mockito.when(userService.get(1)).thenReturn(
37+
new UserVO().setId(1).setUsername("username:1"));
38+
System.out.println("after mock:" + userService.get(1));
39+
40+
// 查询用户列表
41+
ResultActions resultActions = mvc.perform(MockMvcRequestBuilders.get("/users/v2/1"));
42+
// 校验结果
43+
resultActions.andExpect(MockMvcResultMatchers.status().isOk()); // 响应状态码 200
44+
resultActions.andExpect(MockMvcResultMatchers.content().json("{\n" +
45+
" \"id\": 1,\n" +
46+
" \"username\": \"username:1\"\n" +
47+
"}")); // 响应结果
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package cn.iocoder.springboot.lab23.springmvc;

lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab21/springmvc/core/package-info.java

-4
This file was deleted.

lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab21/springmvc/Application.java lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab23/springmvc/Application.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cn.iocoder.springboot.lab21.springmvc;
1+
package cn.iocoder.springboot.lab23.springmvc;
22

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

lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab21/springmvc/constants/ServiceExceptionEnum.java lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab23/springmvc/constants/ServiceExceptionEnum.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cn.iocoder.springboot.lab21.springmvc.constants;
1+
package cn.iocoder.springboot.lab23.springmvc.constants;
22

33
/**
44
* 业务异常枚举

lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab21/springmvc/controller/UserController.java lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab23/springmvc/controller/UserController.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package cn.iocoder.springboot.lab21.springmvc.controller;
1+
package cn.iocoder.springboot.lab23.springmvc.controller;
22

3-
import cn.iocoder.springboot.lab21.springmvc.constants.ServiceExceptionEnum;
4-
import cn.iocoder.springboot.lab21.springmvc.core.exception.ServiceException;
5-
import cn.iocoder.springboot.lab21.springmvc.core.vo.CommonResult;
6-
import cn.iocoder.springboot.lab21.springmvc.vo.UserVO;
3+
import cn.iocoder.springboot.lab23.springmvc.constants.ServiceExceptionEnum;
4+
import cn.iocoder.springboot.lab23.springmvc.core.exception.ServiceException;
5+
import cn.iocoder.springboot.lab23.springmvc.core.vo.CommonResult;
6+
import cn.iocoder.springboot.lab23.springmvc.vo.UserVO;
77
import org.springframework.web.bind.annotation.GetMapping;
88
import org.springframework.web.bind.annotation.RequestMapping;
99
import org.springframework.web.bind.annotation.RequestParam;

lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab21/springmvc/controller2/TestController.java lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab23/springmvc/controller2/TestController.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package cn.iocoder.springboot.lab21.springmvc.controller2;
1+
package cn.iocoder.springboot.lab23.springmvc.controller2;
22

3-
import cn.iocoder.springboot.lab21.springmvc.vo.UserVO;
3+
import cn.iocoder.springboot.lab23.springmvc.vo.UserVO;
4+
import cn.iocoder.springboot.lab23.springmvc.core.web.GlobalResponseBodyHandler;
45
import org.springframework.web.bind.annotation.GetMapping;
56
import org.springframework.web.bind.annotation.RequestMapping;
67
import org.springframework.web.bind.annotation.RestController;
@@ -10,7 +11,7 @@
1011
/**
1112
* 测试 Controller
1213
*
13-
* 这个类的目的,主要是为了测试 {@link cn.iocoder.springboot.lab21.springmvc.core.web.GlobalResponseBodyHandler} ,不拦截处理这个包
14+
* 这个类的目的,主要是为了测试 {@link GlobalResponseBodyHandler} ,不拦截处理这个包
1415
*/
1516
@RestController
1617
@RequestMapping("/test")

lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab21/springmvc/core/exception/ServiceException.java lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab23/springmvc/core/exception/ServiceException.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package cn.iocoder.springboot.lab21.springmvc.core.exception;
1+
package cn.iocoder.springboot.lab23.springmvc.core.exception;
22

3-
import cn.iocoder.springboot.lab21.springmvc.constants.ServiceExceptionEnum;
3+
import cn.iocoder.springboot.lab23.springmvc.constants.ServiceExceptionEnum;
44

55
/**
66
* 服务异常
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* 提供核心封装
3+
*/
4+
package cn.iocoder.springboot.lab23.springmvc.core;

lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab21/springmvc/core/vo/CommonResult.java lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab23/springmvc/core/vo/CommonResult.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cn.iocoder.springboot.lab21.springmvc.core.vo;
1+
package cn.iocoder.springboot.lab23.springmvc.core.vo;
22

33
import com.fasterxml.jackson.annotation.JsonIgnore;
44
import org.springframework.util.Assert;

lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab21/springmvc/core/web/GlobalExceptionHandler.java lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab23/springmvc/core/web/GlobalExceptionHandler.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package cn.iocoder.springboot.lab21.springmvc.core.web;
1+
package cn.iocoder.springboot.lab23.springmvc.core.web;
22

3-
import cn.iocoder.springboot.lab21.springmvc.constants.ServiceExceptionEnum;
4-
import cn.iocoder.springboot.lab21.springmvc.core.exception.ServiceException;
5-
import cn.iocoder.springboot.lab21.springmvc.core.vo.CommonResult;
3+
import cn.iocoder.springboot.lab23.springmvc.constants.ServiceExceptionEnum;
4+
import cn.iocoder.springboot.lab23.springmvc.core.exception.ServiceException;
5+
import cn.iocoder.springboot.lab23.springmvc.core.vo.CommonResult;
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
88
import org.springframework.web.bind.MissingServletRequestParameterException;

lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab21/springmvc/core/web/GlobalResponseBodyHandler.java lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab23/springmvc/core/web/GlobalResponseBodyHandler.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package cn.iocoder.springboot.lab21.springmvc.core.web;
1+
package cn.iocoder.springboot.lab23.springmvc.core.web;
22

3-
import cn.iocoder.springboot.lab21.springmvc.core.vo.CommonResult;
3+
import cn.iocoder.springboot.lab23.springmvc.core.vo.CommonResult;
44
import org.springframework.core.MethodParameter;
55
import org.springframework.http.MediaType;
66
import org.springframework.http.server.ServerHttpRequest;

lab-23/lab-springmvc-23-01/src/main/java/cn/iocoder/springboot/lab21/springmvc/vo/UserVO.java lab-23/lab-springmvc-23-02/src/main/java/cn/iocoder/springboot/lab23/springmvc/vo/UserVO.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cn.iocoder.springboot.lab21.springmvc.vo;
1+
package cn.iocoder.springboot.lab23.springmvc.vo;
22

33
/**
44
* 用户 VO

lab-23/lab-springmvc-23-02/src/test/java/cn/iocoder/springboot/lab21/springmvc/package-info.java

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package cn.iocoder.springboot.lab23.springmvc;

0 commit comments

Comments
 (0)