Class ReactiveRoutes


  • public class ReactiveRoutes
    extends Object
    Provides utility methods, mainly to handle text/event-stream responses.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  ReactiveRoutes.ServerSentEvent<T>
      A class allowing to customized how the server sent events are written.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> io.smallrye.mutiny.Multi<T> asEventStream​(io.smallrye.mutiny.Multi<T> multi)
      Indicates the the given stream should be written as server-sent-event in the response.
      static <T> io.smallrye.mutiny.Multi<T> asJsonArray​(io.smallrye.mutiny.Multi<T> multi)
      Indicates the the given stream should be written as a chunked JSON array in the response.
      static <T> io.smallrye.mutiny.Multi<T> asJsonStream​(io.smallrye.mutiny.Multi<T> multi)
      Indicates the the given stream should be written as a Json stream in the response.
    • Method Detail

      • asEventStream

        public static <T> io.smallrye.mutiny.Multi<T> asEventStream​(io.smallrye.mutiny.Multi<T> multi)
        Indicates the the given stream should be written as server-sent-event in the response. Returning a multi wrapped using this method produces a text/event-stream response. Each item is written as an event in the response. The response automatically enables the chunked encoding and set the content type.

        If the item is a String, the data part of the event is this string. An id is automatically generated. If the item is a Buffer, the data part of the event is this buffer. An id is automatically generated. If the item is an Object, the data part of the event is the JSON representation of this object. An id is automatically generated. If the item is an ReactiveRoutes.ServerSentEvent, the data part of the event is the JSON representation of this ReactiveRoutes.ServerSentEvent.data(). The id is computed from ReactiveRoutes.ServerSentEvent.id() (generated if not implemented). The event section (ignored in all the other case) is computed from ReactiveRoutes.ServerSentEvent.event().

        Example of usage:

         @Route(path = "/people")
         Multi<Person> people(RoutingContext context) {
             return ReactiveRoutes.asEventStream(Multi.createFrom().items(
                     new Person("superman", 1),
                     new Person("batman", 2),
                     new Person("spiderman", 3)));
         }
         
        Type Parameters:
        T - the type of item, can be string, buffer, object or io.quarkus.vertx.web.ReactiveRoutes.ServerSentEvent
        Parameters:
        multi - the multi to be written
        Returns:
        the wrapped multi
      • asJsonStream

        public static <T> io.smallrye.mutiny.Multi<T> asJsonStream​(io.smallrye.mutiny.Multi<T> multi)
        Indicates the the given stream should be written as a Json stream in the response. Returning a multi wrapped using this method produces a application/x-ndjson response. Each item is written as an serialized json on a new line in the response. The response automatically enables the chunked encoding and set the content type.

        If the item is a String, the content will be wrapped in quotes and written. If the item is an Object, then the JSON representation of this object will be written.

        Example of usage:

         @Route(path = "/people")
         Multi<Person> people(RoutingContext context) {
             return ReactiveRoutes.asJsonStream(Multi.createFrom().items(
                     new Person("superman", 1),
                     new Person("batman", 2),
                     new Person("spiderman", 3)));
         }
         
        This example produces:
          {"name":"superman", "id":1}
          {...}
          {...}
         
        Type Parameters:
        T - the type of item, can be string, object
        Parameters:
        multi - the multi to be written
        Returns:
        the wrapped multi
      • asJsonArray

        public static <T> io.smallrye.mutiny.Multi<T> asJsonArray​(io.smallrye.mutiny.Multi<T> multi)
        Indicates the the given stream should be written as a chunked JSON array in the response. Returning a multi wrapped using this method produces a application/json response. Each item is written as an JSON object in the response. The response automatically enables the chunked encoding and set the content type.

        If the item is a String, the content is written in the array. If the item is an Object, the content is transformed to JSON and written in the array.

        Note that the array is written in the response item by item, without accumulating the data. Example of usage:

         @Route(path = "/people")
         Multi<Person> people(RoutingContext context) {
             return ReactiveRoutes.asJsonArray(Multi.createFrom().items(
                     new Person("superman", 1),
                     new Person("batman", 2),
                     new Person("spiderman", 3)));
         }
         
        This example produces: [{"name":"superman", "id":1}, {...}, {..,}]
        Type Parameters:
        T - the type of item, can be string or object
        Parameters:
        multi - the multi to be written
        Returns:
        the wrapped multi