Skip to content

Commit e7efd57

Browse files
author
YunaiV
committed
增加 spring boot + feign 示例
1 parent b6a8639 commit e7efd57

File tree

8 files changed

+26
-10
lines changed

8 files changed

+26
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
## RPC 开发
5656

5757
* [《芋道 Spring Boot Dubbo 入门》](http://www.iocoder.cn/Spring-Boot/Dubbo/?github) 对应 [lab-30](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-30)
58+
* [《芋道 Spring Boot 声明式调用 Feign 入门》](http://www.iocoder.cn/Spring-Boot/Feign/?github) 对应 [lab-58](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-58)
5859
* 《芋道 Spring Boot Motan 入门》计划中...
5960
* 《芋道 Spring Boot WebService 入门》计划中...
6061
* 《芋道 Spring Boot SOFARPC 入门》计划中...

lab-42/lab-42-demo02/src/main/java/cn/iocoder/springboot/lab23/testdemo/dao/UserDao.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@ public UserDO selectById(Integer id) {
1818
id);
1919
}
2020

21+
public UserDO selectByUsername(String username) {
22+
return new UserDO().setId(1)
23+
.setUsername(username)
24+
.setPassword(null);
25+
}
26+
2127
}

lab-42/lab-42-demo02/src/main/java/cn/iocoder/springboot/lab23/testdemo/service/UserService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public boolean exists(String username) {
2020
}
2121

2222
public Integer add(String username, String password) {
23-
if (exists(username)) {
23+
if (userDao.selectByUsername(username) != null) {
2424
return null;
2525
}
2626
return 1;

lab-42/lab-42-demo02/src/test/java/cn/iocoder/springboot/lab23/testdemo/service/UserServiceTest2.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package cn.iocoder.springboot.lab23.testdemo.service;
22

3+
import cn.iocoder.springboot.lab23.testdemo.dao.UserDao;
34
import org.junit.Assert;
45
import org.junit.Test;
56
import org.junit.runner.RunWith;
67
import org.mockito.Mockito;
8+
import org.springframework.beans.factory.annotation.Autowired;
79
import org.springframework.boot.test.context.SpringBootTest;
810
import org.springframework.boot.test.mock.mockito.SpyBean;
911
import org.springframework.test.context.junit4.SpringRunner;
@@ -12,15 +14,17 @@
1214
@SpringBootTest
1315
public class UserServiceTest2 {
1416

15-
@SpyBean
17+
@Autowired
1618
private UserService userService;
1719

20+
@SpyBean
21+
private UserDao userDao;
22+
1823
@Test
1924
public void testAddSuccess() {
2025
System.out.println("testAddSuccess");
2126
// Mock UserService 的 exists 方法
22-
Mockito.when(userService.exists("username")).thenReturn(
23-
false);
27+
Mockito.when(userDao.selectByUsername("username")).thenReturn(null);
2428

2529
Assert.assertNotNull("注册返回为 null,注册失败",
2630
userService.add("username", "password"));

lab-58/lab-58-feign-demo/src/main/java/cn/iocoder/springboot/lab58/feigndemo/feign/UserServiceFeignClient02.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
import java.util.List;
1111

12+
/**
13+
* 基于 SpringContract 拓展契约
14+
*/
1215
public interface UserServiceFeignClient02 {
1316

1417
// 获得用户详情

lab-58/lab-58-user-service/src/main/java/cn/iocoder/springboot/lab58/userservice/controller/UserController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
@RequestMapping("/user")
1212
public class UserController {
1313

14-
@GetMapping("/get")
14+
@GetMapping("/get") // 获得指定用户
1515
public UserResponse get(@RequestParam("id") Integer id) {
1616
return new UserResponse().setId(id)
1717
.setName("昵称:" + id).setGender(id % 2 == 0 ? 1 : 2);
1818
}
1919

20-
@GetMapping("/list")
20+
@GetMapping("/list") // 获得匹配的用户列表
2121
public List<UserResponse> list(@RequestParam(value = "name", required = false) String name,
2222
@RequestParam(value = "gender", required = false) Integer gender) {
2323
List<UserResponse> users = new ArrayList<>();
@@ -28,7 +28,7 @@ public List<UserResponse> list(@RequestParam(value = "name", required = false) S
2828
return users;
2929
}
3030

31-
@PostMapping("/add")
31+
@PostMapping("/add") // 添加用户
3232
public Integer add(@RequestBody UserAddRequest request) {
3333
System.out.println("昵称:" + request.getName());
3434
System.out.println("性别:" + request.getGender());
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<http://www.iocoder.cn/Spring-Boot/Feign/?github>

pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<!-- <module>lab-39</module>-->
5353
<!-- <module>lab-40</module>-->
5454
<!-- <module>lab-41</module>-->
55-
<module>lab-42</module>
55+
<!-- <module>lab-42</module>-->
5656
<!-- <module>lab-43</module>-->
5757
<!-- <module>lab-44</module>-->
5858
<!-- <module>lab-45</module>-->
@@ -67,6 +67,8 @@
6767
<!-- <module>lab-54</module>-->
6868
<!-- <module>lab-55</module>-->
6969
<!-- <module>lab-56</module>-->
70+
<!-- <module>lab-57</module>-->
71+
<!-- <module>lab-58</module>-->
7072

7173
<!-- Spring Cloud 示例 -->
7274
<!-- <module>labx-01</module>-->
@@ -92,8 +94,7 @@
9294
<!-- <module>labx-21</module>-->
9395
<!-- <module>labx-22</module>-->
9496
<!-- <module>labx-23</module>-->
95-
<!-- <module>lab-57</module>-->
96-
<module>lab-58</module>
97+
9798
</modules>
9899

99100
</project>

0 commit comments

Comments
 (0)