gpt4 book ai didi

java - Micronaut RxHttpClient null

转载 作者:行者123 更新时间:2023-12-02 10:08:49 25 4
gpt4 key购买 nike

我正在测试 RxHttpClient 功能。

我创建了一个在 http://localhost:8086 上运行的简单服务,我正在从 http://localhost:8080 上运行的另一个服务访问该服务。按照 micronaut 文档初始化 RxHttpClient 后,我​​看到 RxHttpClient 仍然为 NULL。这是客户端实现

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");
}
}

可能我在这里遗漏了一些东西,对于这里可能遗漏的内容有什么建议吗?

最佳答案

如果没有看到演示问题的项目,很难说出了什么问题,但我预计项目中有些配置错误,或者您正在自己创建 bean 的实例,而不是让 DI 容器为您创建它。

查看该项目 https://github.com/jeffbrown/mithrandirclient .

https://github.com/jeffbrown/mithrandirclient/blob/2c86d361db42f9beff4bb7620789fd4e422941d0/src/main/java/mithrandirclient/TestClient.java

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";
}
}

https://github.com/jeffbrown/mithrandirclient/blob/master/src/main/java/mithrandirclient/DemoController.java

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();
}
}

当我向 Controller 发送请求时,我得到了预期的结果:

$ curl http://localhost:8080/demo
Some Random Name

服务器控制台显示客户端实际上不null:

getRandomName invoked => io.micronaut.http.client.DefaultHttpClient@39ce2d3d

更新:

提交于https://github.com/jeffbrown/mithrandirclient/commit/0ba0216bca4f31ee3ff296579b829ab4615fa6db使代码更像原始问题中的代码,但结果是相同的。注入(inject)确实有效,并且客户端不为 null

关于java - Micronaut RxHttpClient null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55145912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com