微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用现有服务器创建 Micronaut java httpclient

如何解决使用现有服务器创建 Micronaut java httpclient

我有一个使用以下配置运行的 Micronaut 应用程序:

micronaut:
  server:
    cors:
       enabled: true 
    port: 8080

现在我有一个增强功能,我想调用第 3 方 URL 并在我的应用程序(我的应用程序中的模块之一)中获取响应。我使用了以下代码片段:

    EmbeddedServer server = ApplicationContext.run(EmbeddedServer.class);
    HttpClient client = server .getApplicationContext() .createBean(HttpClient.class,server.getURL());
    HttpRequest req = HttpRequest.GET(urlHost);
    HttpResponse<String> response = client.toBlocking().exchange(req,String.class);

但这行不通。我得到端口已经在使用中。我在谷歌上没有找到太多帮助,因为 Micronaut 的 HttpClient 通常用于 Micronaut 测试,而我的情况不是这样。这可以在我的应用程序中使用它吗?如果是这样怎么办?提前致谢。

解决方法

这是因为您正在通过 --------------------------------------------------------------------------- KeyError Traceback (most recent call last) c:\users\kanishk\appdata\local\programs\python\python39\lib\site-packages\pandas\core\indexes\base.py in get_loc(self,key,method,tolerance) 3079 try: -> 3080 return self._engine.get_loc(casted_key) 3081 except KeyError as err: pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 0 The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) <ipython-input-8-348a6e96ae02> in <module> ----> 1 some_digit = X[0] c:\users\kanishk\appdata\local\programs\python\python39\lib\site-packages\pandas\core\frame.py in __getitem__(self,key) 3022 if self.columns.nlevels > 1: 3023 return self._getitem_multilevel(key) -> 3024 indexer = self.columns.get_loc(key) 3025 if is_integer(indexer): 3026 indexer = [indexer] c:\users\kanishk\appdata\local\programs\python\python39\lib\site-packages\pandas\core\indexes\base.py in get_loc(self,tolerance) 3080 return self._engine.get_loc(casted_key) 3081 except KeyError as err: -> 3082 raise KeyError(key) from err 3083 3084 if tolerance is not None: KeyError: 0 启动另一台服务器。

你不需要它。通过构造函数将 ApplicationContext.run(EmbeddedServer.class) 注入你的类就足够了:

HttpClient

例如,如果您在应用程序配置中的 @Singleton public class MyClient { private final RxHttpClient client; public MyClient(@Client("https://some.third-party.com") RxHttpClient client) { this.client = client; } HttpResponse<String> getSomething(Integer id) { URI uri = UriBuilder.of("/some-objects").path(id).build(); return client.toBlocking().exchange(HttpRequest.GET(uri),String.class); } } 路径下有第三方服务器 URL,那么您可以使用 some-service.url


另一种选择是为第三方服务器定义声明性客户端,然后在需要的地方将其注入到您的类中。

首先为您的第三方服务定义客户端接口:

@Client("${some-service.url}")

在应用程序配置 (application.yaml) 中为该服务添加客户端配置:

@Client("some-service")
public interface SomeServiceClient {

    @Get("/api/some-objects/{id}")
    String getSomeObject(@QueryValue("id") Integer id);
}

然后您可以在需要的地方注入 micronaut: http: services: some-service: url: "https://some.third-party.com" read-timeout: 1m

SomeServiceClient

您可以在 Micronaut 文档中找到更多信息 https://guides.micronaut.io/latest/micronaut-http-client-gradle-java.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。