I have a simple program with two parts: a Spring 5 server with one endpoint that returns a Mono<Double>, and a client program that reads the value and prints it.
When I browse to http://localhost:8080/rand a double value is returned. However, when I use the client the retrieved value is always null (response status is 200).
What am I missing?
File Main.java
@SpringBootApplication
@EnableWebFlux
public class Main {
public static void main(String... args) {
SpringApplication.run(Main.class);
}
}
File MyController.java
@RestController
public class MyController {
@GetMapping("/rand")
public Mono<Double> GetDouble() {
return Mono.just(ThreadLocalRandom.current().nextDouble());
}
}
File 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);
}
}