小编Tat*_*ute的帖子

Quarkus & Microprofile :有没有更好的方法将 application.properties 中的属性使用到 @ClientHeaderParam 中?

我正在尝试构建一个简单的应用程序,该应用程序使用quarkus-rest-client. 我必须注入一个 API 密钥作为标头,这对于 API 的所有资源都是相同的。所以我想把这个 API Key 的值(取决于环境dev/qa/prod)放在application.properties位于src/main/resources.

我尝试了不同的方法来实现这一目标:

  • 直接使用com.acme.Configuration.getKey@ClientHeaderParamvalue 属性
  • 创建一个 StoresClientHeadersFactory 类,该类实现 ClientHeadersFactory 接口以注入配置

最后,我找到了下面描述的方法来使它工作。

我的问题是:有没有更好的方法来做到这一点?

这是我的代码:

  • StoreService.java这是我访问 API 的客户端
@Path("/stores")
@RegisterRestClient
@ClientHeaderParam(name = "ApiKey", value = "{com.acme.Configuration.getStoresApiKey}")
public interface StoresService {

    @GET
    @Produces("application/json")
    Stores getStores();

}
Run Code Online (Sandbox Code Playgroud)
  • 配置文件
@ApplicationScoped
public class Configuration {

    @ConfigProperty(name = "apiKey.stores")
    private String storesApiKey;

    public String getKey() {
        return storesApiKey;
    }

    public static String getStoresApiKey() {
        return CDI.current().select(Configuration.class).get().getKey(); …
Run Code Online (Sandbox Code Playgroud)

java client microprofile quarkus

5
推荐指数
2
解决办法
2485
查看次数

标签 统计

client ×1

java ×1

microprofile ×1

quarkus ×1