首页 > 解决方案 > Quarkus:对 URL 的请求以字符串形式给出

问题描述

我想用 Quarkus 发出一个 HTTP 请求。但是,目标 URL 在编译时是未知的,它将在运行时由不同的部分组成。

Quarkus 提供了一种构建静态 REST 客户端的方法,如下所示:

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

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

但是,我正在寻找类似 Pythonrequests包的东西:

url = 'https://stackoverflow.com/questions/ask'
response = requests.get(url)

我正在 Kotlin 中构建应用程序,因此所有 Java 和 Kotlin 库都应该可以工作。

我应该使用什么?

标签: javarestkotlinquarkus

解决方案


通过在接口中定义 MP REST 客户端,您可以使用程序化客户端创建 API:

CountriesService remoteApi = RestClientBuilder.newBuilder()
        .baseUri("created at runtime url")
        .build(CountriesService.class);
remoteApi.getByName("");

推荐阅读