4

文档显示 Http 客户端应该设置一个 URL 或一个服务 ID 来实例化:

@Client("https://api.twitter.com/1.1") 
@Inject 
RxHttpClient httpClient;

如何使用一个 HttpClient 实例请求多个不同域的 URL?

@Client 
@Inject 
RxHttpClient httpClient;

httpClient.retrieve( HttpRequest.GET("https://twitter.com/") )
httpClient.retrieve( HttpRequest.GET("https://www.google.com/") )

代码将返回错误: No value specified for @Client\r\nPath Taken

4

1 回答 1

0

The HttpClient and subsequent RxHttpClient in Micronaut are scoped to a given URL. They maintain some configuration, load balancing, scheduling, SSL contextual information and HTTP client filters scoped to the configured URL. In your example you would need to inject two different clients.

@Inject 
@Client("https://api.twitter.com/1.1") 
RxHttpClient twitterClient;

@Inject 
@Client("https://www.google.com") 
RxHttpClient googleClient;

twitterClient.retrieve( HttpRequest.GET("/statuses/user_timeline") )
googleClient.retrieve( HttpRequest.GET("/some/available/endpoint") )

Also note the Injected RxHttpClient can be setup to use a configuration path if desired.

于 2018-08-01T18:09:30.933 回答