I am testing the RxHttpClient capabilities.
I have created a simple service running on http://localhost:8086
, which I am accessing from another service running on http://localhost:8080
. After following the micronaut documentation to initialize the RxHttpClient, I see that the RxHttpClient is still NULL. Here is the client implementation
import io.micronaut.http.client.RxHttpClient;
import io.micronaut.http.client.annotation.Client;
import io.reactivex.Flowable;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class TestClient {
@Inject
@Client("http://localhost:8086")
RxHttpClient httpClient;
public Flowable<String> getRandomName(){
System.out.println("getRandomName invoked => " + httpClient);
return Flowable.just("test");
}
}
Probably I am missing something here, any suggestions as to what could be missing here?
It is hard to say what is wrong without seeing a project which demonstrates the problem but I expect something is misconfigured in the project or you are creating an instance of the bean yourself instead of letting the DI container create it for you.
See the project at https://github.com/jeffbrown/mithrandirclient.
package mithrandirclient;
import io.micronaut.http.client.RxHttpClient;
import io.micronaut.http.client.annotation.Client;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class TestClient {
@Inject
@Client("http://localhost:8086")
RxHttpClient httpClient;
public String getRandomName(){
System.out.println("getRandomName invoked => " + httpClient);
return "Some Random Name";
}
}
package mithrandirclient;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
@Controller("/demo")
public class DemoController {
private TestClient testClient;
public DemoController(TestClient testClient) {
this.testClient = testClient;
}
@Get("/")
public String index() {
return testClient.getRandomName();
}
}
When I send a request to the controller, I get the expected result:
$ curl http://localhost:8080/demo
Some Random Name
The server console shows that the client is in fact not null
:
getRandomName invoked => io.micronaut.http.client.DefaultHttpClient@39ce2d3d
UPDATE:
The commit at https://github.com/jeffbrown/mithrandirclient/commit/0ba0216bca4f31ee3ff296579b829ab4615fa6db makes the code more like the code in the original question, but the result is the same. The injection does work and the client is not null
.