2

嗨,我是 openfeign 的新手,我正在尝试使用构建 fein 客户端的控制器类,并且想使用配置文件中的“baseurl”,但它不是从配置文件中读取它,而是在其他部分读取它在项目的调试模式下,这似乎甚至在配置类准备好之前就已经命中,请帮助。

@Data
@Component
public class MyController {

  private MyTokenClient tokenClient;

  private MyClient MyClient;

  //TBD read from db
  private String MyUserId;
  private String MyPassword;

  // need to get this value from config
  @Value("${My.base-url}")
  private String baseUrl = "https://api.test.annabanana.com";


  @Autowired
  public MyController() {

    this.tokenClient = Feign.builder().contract(new SpringMvcContract())
      .encoder(new GsonEncoder())
      .decoder(new GsonDecoder())
      .logger(new Slf4jLogger(MyTokenClient.class))
      .logLevel(feign.Logger.Level.FULL)
      .requestInterceptor(new BasicAuthRequestInterceptor("USER", "PASSWORD"))
      .target(MyTokenClient.class, baseUrl);

    this.MyClient = Feign.builder().contract(new SpringMvcContract())
      .requestInterceptor(new MyRequestInterceptor()).target(MyClient.class, baseUrl);

  }

}

4

1 回答 1

1

尝试一下

@Component
public class MyController {

  private final MyClient MyClient;

  private MyTokenClient tokenClient;
  private String MyUserId;
  private String MyPassword;

  @Autowired
  public MyController(@Value("${My.base-url}") String baseUrl) {
    this.tokenClient = Feign.builder().contract(new SpringMvcContract())
      .encoder(new GsonEncoder())
      .decoder(new GsonDecoder())
      .logger(new Slf4jLogger(MyTokenClient.class))
      .logLevel(feign.Logger.Level.FULL)
      .requestInterceptor(new BasicAuthRequestInterceptor("USER", "PASSWORD"))
      .target(MyTokenClient.class, baseUrl);
    this.MyClient = Feign.builder().contract(new SpringMvcContract())
      .requestInterceptor(new MyRequestInterceptor()).target(MyClient.class, baseUrl);
  }
}

不要使用@Data,如果它不是 dto

于 2020-01-02T19:42:02.310 回答