问题描述
我需要从 Quarkus 应用程序发送 HTTP 请求。在 this guide 之后,我有这个 RestClient:
@Path("/v1")
@RegisterRestClient
public interface CountriesService {
@GET
@Path("/name/{name}")
Set<Country> getByName(@PathParam String name);
}
在 Path
注释中,我可以配置路径。但是根据 this paragraph.
# 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);
}
}
您无法通过使用 @RegisterRestClient
注释创建的客户端来实现这一点