使用 Quarkus & Resteasy 在运行时定义客户端目标 URL

问题描述

我需要从 Quarkus 应用程序发送 HTTP 请求。在 this guide 之后,我有这个 RestClient:

@Path("/v1")
@RegisterRestClient
public interface CountriesService {

    @GET
    @Path("/name/{name}")
    Set<Country> getByName(@PathParam String name);
}

Path 注释中,我可以配置路径。但是根据 this paragraph.

,要调用的域/url 是在配置文件中定义的
# Your configuration properties
org.acme.rest.client.CountriesService/mp-rest/url=https://restcountries.eu/rest # 
org.acme.rest.client.CountriesService/mp-rest/scope=javax.inject.Singleton #

就我而言,我需要在运行时以编程方式定义此 URL,因为我将其作为回调 URL 接收。

有没有办法做到这一点?

解决方法

Quarkus Rest Client 和 Quarkus Rest Client Reactive 实现了 MicroProfile Rest 规范,因此允许以编程方式使用 RestClientBuilder 创建客户端 存根,例如:

public class SomeService {
   public Response doWorkAgainstApi(URI apiUri,ApiModel apiModel) {
       RemoteApi remoteApi = RestClientBuilder.newBuilder()
            .baseUri(apiUri)
            .build(RemoteApi.class);
       return remoteApi.execute(apiModel);
   }
}

https://download.eclipse.org/microprofile/microprofile-rest-client-2.0/microprofile-rest-client-spec-2.0.html#_sample_builder_usage

您无法通过使用 @RegisterRestClient 注释创建的客户端来实现这一点