可以在 HTTP micronaut 声明式客户端中更改服务器吗?

It's possible to change server in a HTTP micronaut declarative client?

我正在使用 Micronaut 的声明性 http 客户端从 API 检索数据。但是现在我需要在运行时动态更改服务器地址。有可能吗?

示例:

@Client("${http.client.url}")
@Header(name="Accept-Encoding", value="gzip, deflate, br")
public interface CatalogClientApi {

可以通过某种方式更改“${http.client.url}”吗?或者我必须切换到低级别的 http 客户端?

你必须切换。 Micronaut 中的注解是在编译时处理的。

@Client 注解正在注入 RxHttpClient。

您可以使用声明式方法来完成

URL url = new URL("http://your-url-here.com");
RxHttpClient client = RxHttpClient.create(url);

然后我找到了一个简单的解决方案(有点难看):

@Client("/")
@Header(name="Accept-Encoding", value="gzip, deflate, br")
public interface ExampleApi {
    @Post("{+path}/V1/products")
String post(@PathVariable String path, @Header("Authorization") String token, Body product);

“@Client”有一个/占位符,然后服务器将在{+path}中使用的路径变量中。 我发现在这里使用主机有点误导,但它运行良好。