Class WebfluxFun
java.lang.Object
dmx.fun.spring.webflux.WebfluxFun
Maps dmx-fun outcomes to Spring WebFlux
ServerResponse for functional endpoints,
so a handler can return Result, Try, Option, or Validated
without re-implementing status/body conventions per service.
Default HTTP mapping
Result.Ok(v)/Try.Success(v)/Option.Some(v)/Validated.Valid(v)→200 OKwithvas the bodyOption.None→404 Not FoundValidated.Invalid(errors)→400 Bad Requestwith the accumulated errors (as aList) as the bodyResult.Err(e)→ mapped by the suppliedErrorHttpMapperTry.Failure(t)→ mapped by the suppliedThrowableHttpMapper- an empty source
Mono(it completes without emitting an outcome) →404 Not Found
The success body is written with
ServerResponse.ok().bodyValue(v), so the value must be encodable by the
application's WebFlux codecs. Compose with fun-reactor's ReactorResult
operators on the Mono<Result<...>> before calling these adapters to stay on the
Result track.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <V> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> fromOption(reactor.core.publisher.Mono<Option<V>> source) Maps aMono<Option<V>>to a response:Some→200with the value,None(or empty) →404.static <V> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> fromOption(reactor.core.publisher.Mono<Option<V>> source, SuccessHttpMapper<V> successMapper) Maps aMono<Option<V>>to a response with full control over the present branch:Some→successMapper,None(or empty) →404.static <V,E> reactor.core.publisher.Mono <org.springframework.web.reactive.function.server.ServerResponse> fromResult(reactor.core.publisher.Mono<Result<V, E>> source, ErrorHttpMapper<E> errorMapper) Maps aMono<Result<V, E>>to a response:Ok→200with the value,Err→errorMapper, empty →404.static <V,E> reactor.core.publisher.Mono <org.springframework.web.reactive.function.server.ServerResponse> fromResult(reactor.core.publisher.Mono<Result<V, E>> source, SuccessHttpMapper<V> successMapper, ErrorHttpMapper<E> errorMapper) Maps aMono<Result<V, E>>to a response with full control over the success branch:Ok→successMapper(e.g.static <V,E> reactor.core.publisher.Mono <org.springframework.web.reactive.function.server.ServerResponse> fromResultStream(reactor.core.publisher.Flux<Result<V, E>> source, ErrorHttpMapper<E> errorMapper) Aggregates aFlux<Result<V, E>>into a single response by sequencing it fail-fast (viafun-reactor'sReactorFlux.sequence): allOk→200with the list of values, the firstErr→errorMapper.static <V,E> reactor.core.publisher.Mono <org.springframework.web.reactive.function.server.ServerResponse> fromResultStreamAccumulating(reactor.core.publisher.Flux<Result<V, E>> source) Aggregates aFlux<Result<V, E>>into a single response by accumulating it (viafun-reactor'sReactorFlux.collectValidated): allOk→200with the list of values, anyErr→400with every accumulated error as the body.static <V> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> fromTry(reactor.core.publisher.Mono<Try<V>> source, SuccessHttpMapper<V> successMapper, ThrowableHttpMapper failureMapper) Maps aMono<Try<V>>to a response with full control over the success branch:Success→successMapper,Failure→failureMapper, empty →404.static <V> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> fromTry(reactor.core.publisher.Mono<Try<V>> source, ThrowableHttpMapper failureMapper) Maps aMono<Try<V>>to a response:Success→200with the value,Failure→failureMapper, empty →404.static <V,E> reactor.core.publisher.Mono <org.springframework.web.reactive.function.server.ServerResponse> fromValidated(reactor.core.publisher.Mono<Validated<NonEmptyList<E>, V>> source) Maps aMono<Validated<NonEmptyList<E>, V>>to a response:Valid→200with the value,Invalid→400with the accumulated errors as the body, empty →404.static <V,E> reactor.core.publisher.Mono <org.springframework.web.reactive.function.server.ServerResponse> fromValidated(reactor.core.publisher.Mono<Validated<NonEmptyList<E>, V>> source, SuccessHttpMapper<V> successMapper) Maps aMono<Validated<NonEmptyList<E>, V>>to a response with full control over the valid branch:Valid→successMapper,Invalid→400with the accumulated errors as the body, empty →404.static <V> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> stream(reactor.core.publisher.Flux<V> source, org.springframework.http.MediaType mediaType, Class<V> elementType) Streams aFlux<V>as the response body with the givenmediaType(for exampleMediaType.APPLICATION_NDJSONorMediaType.TEXT_EVENT_STREAM), encoding each element as it arrives instead of collecting first.static <V> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> stream(reactor.core.publisher.Flux<V> source, org.springframework.http.MediaType mediaType, Class<V> elementType, Function<? super Throwable, V> onError) Streams aFlux<V>as the response body, appending a typed fallback element produced byonErrorif the stream terminates with an error — a graceful degradation that keeps the (already-sent)200status instead of abruptly closing the connection.
-
Method Details
-
fromResult
public static <V,E> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> fromResult(reactor.core.publisher.Mono<Result<V, E>> source, ErrorHttpMapper<E> errorMapper) Maps aMono<Result<V, E>>to a response:Ok→200with the value,Err→errorMapper, empty →404.- Type Parameters:
V- the success value typeE- the error type- Parameters:
source- the upstream outcomeerrorMapper- renders aResult.Errvalue as a response- Returns:
- the HTTP response
-
fromResult
public static <V,E> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> fromResult(reactor.core.publisher.Mono<Result<V, E>> source, SuccessHttpMapper<V> successMapper, ErrorHttpMapper<E> errorMapper) Maps aMono<Result<V, E>>to a response with full control over the success branch:Ok→successMapper(e.g.201 Createdwith aLocationheader),Err→errorMapper, empty →404.- Type Parameters:
V- the success value typeE- the error type- Parameters:
source- the upstream outcomesuccessMapper- renders aResult.Okvalue as a responseerrorMapper- renders aResult.Errvalue as a response- Returns:
- the HTTP response
-
fromOption
public static <V> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> fromOption(reactor.core.publisher.Mono<Option<V>> source) Maps aMono<Option<V>>to a response:Some→200with the value,None(or empty) →404.- Type Parameters:
V- the value type- Parameters:
source- the upstream option- Returns:
- the HTTP response
-
fromOption
public static <V> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> fromOption(reactor.core.publisher.Mono<Option<V>> source, SuccessHttpMapper<V> successMapper) Maps aMono<Option<V>>to a response with full control over the present branch:Some→successMapper,None(or empty) →404.- Type Parameters:
V- the value type- Parameters:
source- the upstream optionsuccessMapper- renders the present value as a response- Returns:
- the HTTP response
-
fromTry
public static <V> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> fromTry(reactor.core.publisher.Mono<Try<V>> source, ThrowableHttpMapper failureMapper) Maps aMono<Try<V>>to a response:Success→200with the value,Failure→failureMapper, empty →404.- Type Parameters:
V- the value type- Parameters:
source- the upstream tryfailureMapper- renders aTry.Failurecause as a response- Returns:
- the HTTP response
-
fromTry
public static <V> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> fromTry(reactor.core.publisher.Mono<Try<V>> source, SuccessHttpMapper<V> successMapper, ThrowableHttpMapper failureMapper) Maps aMono<Try<V>>to a response with full control over the success branch:Success→successMapper,Failure→failureMapper, empty →404.- Type Parameters:
V- the value type- Parameters:
source- the upstream trysuccessMapper- renders aTry.Successvalue as a responsefailureMapper- renders aTry.Failurecause as a response- Returns:
- the HTTP response
-
fromValidated
public static <V,E> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> fromValidated(reactor.core.publisher.Mono<Validated<NonEmptyList<E>, V>> source) Maps aMono<Validated<NonEmptyList<E>, V>>to a response:Valid→200with the value,Invalid→400with the accumulated errors as the body, empty →404.- Type Parameters:
V- the value typeE- the element type of the accumulated errors- Parameters:
source- the upstream validated value- Returns:
- the HTTP response
-
fromValidated
public static <V,E> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> fromValidated(reactor.core.publisher.Mono<Validated<NonEmptyList<E>, V>> source, SuccessHttpMapper<V> successMapper) Maps aMono<Validated<NonEmptyList<E>, V>>to a response with full control over the valid branch:Valid→successMapper,Invalid→400with the accumulated errors as the body, empty →404.- Type Parameters:
V- the value typeE- the element type of the accumulated errors- Parameters:
source- the upstream validated valuesuccessMapper- renders aValidvalue as a response- Returns:
- the HTTP response
-
fromResultStream
public static <V,E> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> fromResultStream(reactor.core.publisher.Flux<Result<V, E>> source, ErrorHttpMapper<E> errorMapper) Aggregates aFlux<Result<V, E>>into a single response by sequencing it fail-fast (viafun-reactor'sReactorFlux.sequence): allOk→200with the list of values, the firstErr→errorMapper.- Type Parameters:
V- the success value typeE- the error type- Parameters:
source- the stream of outcomeserrorMapper- renders the firstErras a response- Returns:
- the HTTP response
-
fromResultStreamAccumulating
public static <V,E> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> fromResultStreamAccumulating(reactor.core.publisher.Flux<Result<V, E>> source) Aggregates aFlux<Result<V, E>>into a single response by accumulating it (viafun-reactor'sReactorFlux.collectValidated): allOk→200with the list of values, anyErr→400with every accumulated error as the body. UnlikefromResultStream(Flux, ErrorHttpMapper), this does not short-circuit on the first failure — it reports them all.- Type Parameters:
V- the success value typeE- the error type- Parameters:
source- the stream of outcomes- Returns:
- the HTTP response
-
stream
public static <V> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> stream(reactor.core.publisher.Flux<V> source, org.springframework.http.MediaType mediaType, Class<V> elementType) Streams aFlux<V>as the response body with the givenmediaType(for exampleMediaType.APPLICATION_NDJSONorMediaType.TEXT_EVENT_STREAM), encoding each element as it arrives instead of collecting first.HTTP note: the
200status and headers are sent before the body streams, so a terminal error cannot change the status. Usestream(Flux, MediaType, Class, java.util.function.Function)to append a typed fallback element on error, or collect first withfromResultStream(Flux, ErrorHttpMapper)/fromResultStreamAccumulating(Flux)when the outcome must drive the status code.- Type Parameters:
V- the element type- Parameters:
source- the stream of elementsmediaType- the response content type (e.g. NDJSON or SSE)elementType- the element class, required by the WebFlux encoder- Returns:
- the streaming HTTP response
-
stream
public static <V> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse> stream(reactor.core.publisher.Flux<V> source, org.springframework.http.MediaType mediaType, Class<V> elementType, Function<? super Throwable, V> onError) Streams aFlux<V>as the response body, appending a typed fallback element produced byonErrorif the stream terminates with an error — a graceful degradation that keeps the (already-sent)200status instead of abruptly closing the connection.- Type Parameters:
V- the element type- Parameters:
source- the stream of elementsmediaType- the response content type (e.g. NDJSON or SSE)elementType- the element class, required by the WebFlux encoderonError- maps a terminal error to a final fallback element- Returns:
- the streaming HTTP response
-