日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

@FeignClient 接口调用

發(fā)布時(shí)間:2024/1/23 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 @FeignClient 接口调用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在項(xiàng)目的啟動文件加入:@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);

????}

}

  

實(shí)例結(jié)構(gòu)如下:

那么有實(shí)體類: User.java

Fengn客戶端:UserFeignClient.java

控制器: MovieController.java調(diào)取第三方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服務(wù)里面user項(xiàng)目的名稱,加入此注解,能直接連接user項(xiàng)目接口

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 {

????// 兩個(gè)坑:1. @GetMapping不支持?? 2. @PathVariable得設(shè)置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控制中心,調(diào)取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);

?????????

????}

}

  

?

總結(jié)

以上是生活随笔為你收集整理的@FeignClient 接口调用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。