I'm learning more about using Spring Webflux and experimenting with testing a simple async webservice call. I've looked at several examples and I can't see what I'm doing wrong. I have a service that makes a call to a third party API and all I want to do is output the Json response returned. I'm not converting the response into model objects just yet but this will be the next step if I can get basics working first. The code doesn't log any of the output of the webservice call and I've also tried sending to System.out::println and that also doesn't work. The output in the test only includes the following log output
023-01-04 00:53:46.622 INFO 19938 --- [ main] c.r.io.service.impl.ListlyServiceImpl : Starting call to Listly API
2023-01-04 00:53:52.395 INFO 19938 --- [ main] c.r.io.service.impl.ListlyServiceImpl : Exiting service call to Listly
However , when I put a break point on
listlyResponse.subscribe(listlyResp ->
log.info(listlyResp));
I can actually see the correct contents of the response from the web service call. Any ideas on what I'm doing wrong? This is the code
@Service
public class ListlyServiceImpl implements ListlyService {
private final static Logger log = LoggerFactory.getLogger(ListlyServiceImpl.class);
private final String baseUrl = "https://list.ly/api/v4";
@Override
public void callListlyService(String searchUrl) {
if (searchUrl == null) {
throw new RuntimeException("Search URL cannot be null");
}
log.info("Starting call to Listly API");
Mono<String> listlyResponse = WebClient.create(baseUrl)
.get()
.uri(uriBuilder -> uriBuilder
.path("/meta")
.queryParam("url","{url}")
.build(searchUrl))
.retrieve()
.bodyToMono(String.class);
listlyResponse.subscribe(listlyResp ->
log.info(listlyResp));
// listlyResponseFlux.subscribe(System.out::println);
log.info("Exiting service call to Listly");
}
}
I'm expecting to be able to output the contents of the web service call to the log output which is not working for some reason.