Class WebfluxFun

java.lang.Object
dmx.fun.spring.webflux.WebfluxFun

@NullMarked public final class WebfluxFun extends Object
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 OK with v as the body
  • Option.None404 Not Found
  • Validated.Invalid(errors)400 Bad Request with the accumulated errors (as a List) as the body
  • Result.Err(e) → mapped by the supplied ErrorHttpMapper
  • Try.Failure(t) → mapped by the supplied ThrowableHttpMapper
  • 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 Type
    Method
    Description
    static <V> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse>
    fromOption(reactor.core.publisher.Mono<Option<V>> source)
    Maps a Mono<Option<V>> to a response: Some200 with 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 a Mono<Option<V>> to a response with full control over the present branch: SomesuccessMapper, 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 a Mono<Result<V, E>> to a response: Ok200 with the value, ErrerrorMapper, 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 a Mono<Result<V, E>> to a response with full control over the success branch: OksuccessMapper (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 a Flux<Result<V, E>> into a single response by sequencing it fail-fast (via fun-reactor's ReactorFlux.sequence): all Ok200 with the list of values, the first ErrerrorMapper.
    static <V,E> reactor.core.publisher.Mono<org.springframework.web.reactive.function.server.ServerResponse>
    fromResultStreamAccumulating(reactor.core.publisher.Flux<Result<V,E>> source)
    Aggregates a Flux<Result<V, E>> into a single response by accumulating it (via fun-reactor's ReactorFlux.collectValidated): all Ok200 with the list of values, any Err400 with 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 a Mono<Try<V>> to a response with full control over the success branch: SuccesssuccessMapper, FailurefailureMapper, 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 a Mono<Try<V>> to a response: Success200 with the value, FailurefailureMapper, 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 a Mono<Validated<NonEmptyList<E>, V>> to a response: Valid200 with the value, Invalid400 with 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 a Mono<Validated<NonEmptyList<E>, V>> to a response with full control over the valid branch: ValidsuccessMapper, Invalid400 with 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 a Flux<V> as the response body with the given mediaType (for example MediaType.APPLICATION_NDJSON or MediaType.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 a Flux<V> as the response body, appending a typed fallback element produced by onError if the stream terminates with an error — a graceful degradation that keeps the (already-sent) 200 status instead of abruptly closing the connection.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 a Mono<Result<V, E>> to a response: Ok200 with the value, ErrerrorMapper, empty → 404.
      Type Parameters:
      V - the success value type
      E - the error type
      Parameters:
      source - the upstream outcome
      errorMapper - renders a Result.Err value 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 a Mono<Result<V, E>> to a response with full control over the success branch: OksuccessMapper (e.g. 201 Created with a Location header), ErrerrorMapper, empty → 404.
      Type Parameters:
      V - the success value type
      E - the error type
      Parameters:
      source - the upstream outcome
      successMapper - renders a Result.Ok value as a response
      errorMapper - renders a Result.Err value 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 a Mono<Option<V>> to a response: Some200 with 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 a Mono<Option<V>> to a response with full control over the present branch: SomesuccessMapper, None (or empty) → 404.
      Type Parameters:
      V - the value type
      Parameters:
      source - the upstream option
      successMapper - 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 a Mono<Try<V>> to a response: Success200 with the value, FailurefailureMapper, empty → 404.
      Type Parameters:
      V - the value type
      Parameters:
      source - the upstream try
      failureMapper - renders a Try.Failure cause 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 a Mono<Try<V>> to a response with full control over the success branch: SuccesssuccessMapper, FailurefailureMapper, empty → 404.
      Type Parameters:
      V - the value type
      Parameters:
      source - the upstream try
      successMapper - renders a Try.Success value as a response
      failureMapper - renders a Try.Failure cause 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 a Mono<Validated<NonEmptyList<E>, V>> to a response: Valid200 with the value, Invalid400 with the accumulated errors as the body, empty → 404.
      Type Parameters:
      V - the value type
      E - 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 a Mono<Validated<NonEmptyList<E>, V>> to a response with full control over the valid branch: ValidsuccessMapper, Invalid400 with the accumulated errors as the body, empty → 404.
      Type Parameters:
      V - the value type
      E - the element type of the accumulated errors
      Parameters:
      source - the upstream validated value
      successMapper - renders a Valid value 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 a Flux<Result<V, E>> into a single response by sequencing it fail-fast (via fun-reactor's ReactorFlux.sequence): all Ok200 with the list of values, the first ErrerrorMapper.
      Type Parameters:
      V - the success value type
      E - the error type
      Parameters:
      source - the stream of outcomes
      errorMapper - renders the first Err as 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 a Flux<Result<V, E>> into a single response by accumulating it (via fun-reactor's ReactorFlux.collectValidated): all Ok200 with the list of values, any Err400 with every accumulated error as the body. Unlike fromResultStream(Flux, ErrorHttpMapper), this does not short-circuit on the first failure — it reports them all.
      Type Parameters:
      V - the success value type
      E - 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 a Flux<V> as the response body with the given mediaType (for example MediaType.APPLICATION_NDJSON or MediaType.TEXT_EVENT_STREAM), encoding each element as it arrives instead of collecting first.

      HTTP note: the 200 status and headers are sent before the body streams, so a terminal error cannot change the status. Use stream(Flux, MediaType, Class, java.util.function.Function) to append a typed fallback element on error, or collect first with fromResultStream(Flux, ErrorHttpMapper) / fromResultStreamAccumulating(Flux) when the outcome must drive the status code.

      Type Parameters:
      V - the element type
      Parameters:
      source - the stream of elements
      mediaType - 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 a Flux<V> as the response body, appending a typed fallback element produced by onError if the stream terminates with an error — a graceful degradation that keeps the (already-sent) 200 status instead of abruptly closing the connection.
      Type Parameters:
      V - the element type
      Parameters:
      source - the stream of elements
      mediaType - the response content type (e.g. NDJSON or SSE)
      elementType - the element class, required by the WebFlux encoder
      onError - maps a terminal error to a final fallback element
      Returns:
      the streaming HTTP response