OpenFeign 控制器中的配置值为空

config values are null in the OpenFeign controller

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

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

  }

}

试一试

@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