我有一个简单的程序,由两部分组成:一个 Spring 5 服务器,其一个端点返回 Mono
当我浏览到http://localhost:8080/rand时返回 double 值。但是,当我使用客户端时,检索到的值始终为空(响应状态为200)。
我错过了什么?
文件Main.java
@SpringBootApplication
@EnableWebFlux
public class Main {
public static void main(String... args) {
SpringApplication.run(Main.class);
}
}
文件 MyController.java
@RestController
public class MyController {
@GetMapping("/rand")
public Mono<Double> GetDouble() {
return Mono.just(ThreadLocalRandom.current().nextDouble());
}
}
文件 MyClient.java
public class MyClient {
public static void main(String... args) {
WebClient client = WebClient.create("http://localhost:8080");
Double value = client.get()
.uri("/rand")
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(Double.class)
.block();
System.out.println("value = " + value);
}
}
最佳答案
这是 Spring Framework 5.0.1 的一个已知问题,请参阅 SPR-16166了解更多信息。
关于spring-boot - 无法从 Flux/Mono 读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47417046/