user1619355
user1619355

Reputation: 459

How to return Mono<ServerResponse>

Please see my question embedded in the comments section of the code below -

// This should return a list of all ids and their name and status info, e.g. as json
public Mono<ServerResponse> f1(String originalId) {

    // this gives list of ids which are comma separated       
    Mono<String> ids = f2(originalId);
    ids.flatMapIterable(line -> Arrays.asList(line.split(COMMA)))
            .doOnNext(id -> {
                Mono<String> idName = f3(id);
                Mono<String> idStatus = f4(id);

                Mono<Tuple2<String, String>> combined = Mono.zip(idName, idStatus);

                // How do i return all the combined Mono tuples as Mono<ServerResponse>
            }).subscribe();

    // Need to return proper Mono - not empty         
    return Mono.empty();
}

Also I am not sure whether I should return a Flux or a Mono as return type of f1() as its a list of ids and their respective values

Upvotes: 0

Views: 4301

Answers (1)

Vasyl Sarzhynskyi
Vasyl Sarzhynskyi

Reputation: 3955

You need to return Mono<ServerResponse>.

public Mono<ServerResponse> f1(String key) {
    Mono<String> ids = f2(key);
    Flux<IdInfo> idInfoFlux = ids.flatMapIterable(line -> Arrays.asList(line.split(",")))
            .flatMap(id -> {
                Mono<String> idName = getIdName(id);
                Mono<String> idStatus = getIdStatus(id);
                return Mono.zip(idName, idStatus, (name, status) -> new IdInfo(name, status));
            })
            .doOnNext(id -> System.out.println(id));
    return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(idInfoFlux, IdInfo.class);
}

// your DTO class that you would like to pass in response as json
public static class IdInfo {
    String idName;
    String idStatus;

    public IdInfo(String idName, String idStatus) {
        this.idName = idName;
        this.idStatus = idStatus;
    }

    public String toString() {
        return String.format("IdInfo [idName=%s, idStatus=%s]", idName, idStatus);
    }
}

Upvotes: 2

Related Questions