首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Quarkus & Microprofile :是否有更好的方法将属性从application.properties中使用到@ClientHeaderParam?

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

Stack Overflow用户
提问于 2019-10-25 12:34:00
回答 2查看 5.2K关注 0票数 6

我正在尝试构建一个简单的应用程序,它用quarkus-rest-client调用API。我必须插入一个API密钥作为头,这对于API的所有资源都是一样的。因此,我想将这个API键(取决于环境dev/qa/prod)的值放在位于src/main/resources中的application.properties文件中。

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

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

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

我的问题是:有更好的方法吗?

这是我的密码:

  • StoreService.java,这是我的客户端来访问API。
代码语言:javascript
复制
@Path("/stores")
@RegisterRestClient
@ClientHeaderParam(name = "ApiKey", value = "{com.acme.Configuration.getStoresApiKey}")
public interface StoresService {

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

}
  • Configuration.java
代码语言:javascript
复制
@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();
    }

}
  • StoresController.java是REST控制器
代码语言:javascript
复制
@Path("/stores")
public class StoresController {

    @Inject
    @RestClient
    StoresService storesService;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Stores getStores() {
        return storesService.getStores();
    }

}
EN

回答 2

Stack Overflow用户

发布于 2020-05-19 11:53:49

晚到派对了,但把这个放在这里供我参考。使用@ClientHeaderParam和@HeaderParam似乎有区别,因此我进一步研究了它们:根据微剖面文档,可以将值计算方法放在大括号中。该方法可以提取属性值。

有关更多示例,请参见链接。

编辑:我想出的内容与原来的类似,但在接口上使用了默认方法,因此至少可以放弃配置类。另外,使用org.eclipse.microprofile.config.Config和ConfigProvider类获取配置值:

代码语言:javascript
复制
@RegisterRestClient
@ClientHeaderParam(name = "Authorization", value = "{getAuthorizationHeader}")
public interface StoresService {

    default String getAuthorizationHeader(){
        final Config config = ConfigProvider.getConfig();
        return config.getValue("apiKey.stores", String.class);
    }

    @GET
    @Produces("application/json")
    Stores getStores();
票数 8
EN

Stack Overflow用户

发布于 2019-10-30 11:06:49

我将摆脱Configuration类,并使用@HeaderParam将配置属性从rest端点传递给rest客户端。然后,注释将此属性作为HTTP报头发送到远程服务。

有些像这样的事情应该有效:

代码语言:javascript
复制
@Path("/stores")
@RegisterRestClient
public interface StoresService {

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

}

@Path("/stores")
public class StoresController {
    @ConfigProperty(name = "apiKey.stores")
    private String storesApiKey;

    @Inject
    @RestClient
    StoresService storesService;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Stores getStores() {
        return storesService.getStores(storesApiKey);
    }

}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58558635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档