【问题标题】:It's possible to change server in a HTTP micronaut declarative client?可以在 HTTP micronaut 声明式客户端中更改服务器吗?
【发布时间】:2020-03-26 15:13:15
【问题描述】:

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

例子:

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

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

【问题讨论】:

    标签: micronaut


    【解决方案1】:

    @Client 注解正在注入 RxHttpClient。

    您可以使用声明性方法来做到这一点

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

    【讨论】:

      【解决方案2】:

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

      @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} 中使用的路径变量中。 我发现在这里使用主机有点误导,但它运行良好。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-09-14
          • 1970-01-01
          • 2023-04-03
          • 1970-01-01
          • 1970-01-01
          • 2023-04-08
          • 1970-01-01
          • 2018-09-07
          相关资源
          最近更新 更多