Feign二: @FeignClient 接口调用

在项目的启动文件加入:@EnableFeignClients 注解,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
 
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class FeignApp {
 
 
    public static void main(String[] args) {
        SpringApplication.run(FeignApp.class, args);
    }
}

  

实例结构如下:

 

 

 

 

那么有实体类: User.java

Fengn客户端:UserFeignClient.java

控制器: MovieController.java调取第三方user接口

 

User.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.math.BigDecimal;
 
public class User {
 
    private  Long id;
     
    private String username;
     
    private String name;
     
    private int age;
     
    private BigDecimal balance;
 
    public Long getId() {
        return id;
    }
 
    public void setId(Long id) {
        this.id = id;
    }
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public BigDecimal getBalance() {
        return balance;
    }
 
    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }
     
     
     
     
     
}

  

UserFeign客户端

其中:@FeignClient("spring-boot-user"): spring-boot-user是eureka服务里面user项目的名称,加入此注解,能直接连接user项目接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import com.muyang.bootmovie.entity.User;
 
@FeignClient("spring-boot-user")
public interface UserFeignClient {
 
    // 两个坑:1. @GetMapping不支持   2. @PathVariable得设置value
    @RequestMapping(value="/simple/{id}", method=RequestMethod.GET)
    public User findById(@PathVariable("id") Long id);
     
    @RequestMapping(value="/test", method=RequestMethod.POST)
    public User postUser(@RequestBody User user);
}

  

MovieController控制中心,调取UserFeign客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.muyang.bootmovie.entity.User;
import com.muyang.bootmovie.feign.UserFeignClient;
 
@RestController
public class MovieController {
 
    @Autowired
    private UserFeignClient userFeignClient;
     
    @GetMapping("/movie/{id}")
    public User findById(@PathVariable("id") Long id) {
        return this.userFeignClient.findById(id);
    }
     
    @RequestMapping(value="/test", method=RequestMethod.GET)
    public User userPost(User user)
    {
        return this.userFeignClient.postUser(user);
         
    }
}

  

 

posted @   穆晟铭  阅读(37507)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
历史上的今天:
2017-09-29 vpn: 如果你的Windows无法连接L2TP协议的VPN,809错误
点击右上角即可分享
微信分享提示