Feign:复写Feign的默认配置

Feign二:复写Feign的默认配置

Feign二:复写Feign的默认配置

1.在启动文件加入feign注解:@EnableFeignClients

FeignApp.java

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);

    }

}

  

2.新建一个Feign的默认配置类,并创建一个bean,返回: feign.Contract.Default 默认配置

1

2

3

4

5

6

7

8

9

10

11

12

13

import org.springframework.context.annotation.Bean;

 

import feign.Contract;

 

public class FooConfiguration {

 

    @Bean

    public Contract feignContract()

    {

        return new feign.Contract.Default();

    }

     

}

  

3.新建Feign客户端调用类,调用第三方接口的,并引入第2步复写的Feign配置

1

2

3

4

5

6

7

8

9

10

11

12

13

14

import org.springframework.cloud.netflix.feign.FeignClient;

import org.springframework.web.bind.annotation.PathVariable;

 

import com.muyang.bootmovie.configs.FooConfiguration;

import com.muyang.bootmovie.entity.User;

 

import feign.RequestLine;

 

@FeignClient(name="spring-boot-user", configuration=FooConfiguration.class)

public interface UserFeignClient {

 

    @RequestLine("GET /simple/{id}")

    public User findById(@PathVariable("id") Long id);

}

  

4.创建controller类,并调用feign客户端类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import com.muyang.bootmovie.entity.User;

import com.muyang.bootmovie.feign.UserFeignClient;

 

import feign.Param;

 

 

@RestController

public class UserController {

 

    @Autowired

    private UserFeignClient userFeignClient;

     

    @GetMapping("/user/{id}")

    public User findById(@Param("id") Long id) {

        return this.userFeignClient.findById(id);

    }

     

     

}

 

5。一些其他参考

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;

    }

     

     

     

     

     

}

  

pom.xml

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

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

 

    <artifactId>boot-spring-movie-feign-override</artifactId>

    <packaging>jar</packaging>

 

    <parent>

        <groupId>com.muyang</groupId>

        <artifactId>boot-spring-cloud</artifactId>

        <version>0.0.1-SNAPSHOT</version>

    </parent>

 

    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <java.version>1.8</java.version>

    </properties>

 

    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

 

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-eureka</artifactId>

        </dependency>

 

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-actuator</artifactId>

        </dependency>

         

        <dependency>

                <groupId>org.springframework.cloud</groupId>

                <artifactId>spring-cloud-starter-feign</artifactId>

        </dependency>

         

    </dependencies>

 

     

 

 

</project>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值