Droy
Droy

Reputation: 183

Unable to convert from Flux<String> to List<String>

I'm using Spring webflux in my project for communicating with external API. In my project I'm not able to convert Flux to List.

On trying to do the same with collectList().block() all the elements of the flux gets concatenated to a single string and gets stored at 0th index of list. If I return the Flux instead of List then it sends the expected response. But I need to manipulate the contents and add it as a child to other object & therefore trying to return the List.

public List<String> retrieveWebLogin(String platformId) {
    try {
        ClientResponse response = webClient
                .get()
                .uri(EV_LEGACY_WEB_RTC_ENDPOINT_API_PATH)
                .accept(APPLICATION_JSON)
                .exchange().block();

        Flux<String> uriFlux = response.bodyToFlux(String.class);

        List<String> uriList = uriFlux.collectList().block();
        return uriList;

    } catch (Exception e) {
        logger.info(e.getMessage(), e);
    }
    return null;
}

Expected result: [ "agent1", "agent2" ]

Actual Result: "["agent1","agent2"]"

Upvotes: 2

Views: 2428

Answers (1)

Toerktumlare
Toerktumlare

Reputation: 14712

You code should look like this instead.

final List<String> uriList = webClient
            .get()
            .uri(EV_LEGACY_WEB_RTC_ENDPOINT_API_PATH)
            .accept(MediaType.APPLICATION_JSON_UTF8)
            .exchange()
            .flatMap(response -> response.bodyToMono(new ParameterizedTypeReference<List<String>>() {}))
            .block();

Upvotes: 1

Related Questions