Class ReactiveRoutes


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

      • APPLICATION_JSON

        public static final String APPLICATION_JSON
        The content-type to use to indicate you want to produce an JSON Array response, such as in:
         
         @Route(path = "/heroes", produces = ReactiveRoutes.APPLICATION_JSON_CONTENT_TYPE)
         Multi<Person> heroes() {
             return Multi.createFrom().items(
                     new Person("superman", 1),
                     new Person("batman", 2),
                     new Person("spiderman", 3));
         }
         
         
        Note that the array is streamed object per object. Each object is written individually in the response, until the last one.
        See Also:
        Constant Field Values
      • EVENT_STREAM

        public static final String EVENT_STREAM
        The content-type to use to indicate you want to produce a server-sent-event (SSE) stream response, such as in:
         
         &#64;Route(path = "/heroes", produces = ReactiveRoutes.EVENT_STREAM_CONTENT_TYPE)
         Multi<Person> heroes() {
             return Multi.createFrom().items(
                     new Person("superman", 1),
                     new Person("batman", 2),
                     new Person("spiderman", 3));
         }
         
         
        See Also:
        Constant Field Values
      • ND_JSON

        public static final String ND_JSON
        The content-type to use to indicate you want to produce NDJSON stream response, such as in:
         
         &#64;Route(path = "/heroes", produces = ReactiveRoutes.ND_JSON_CONTENT_TYPE)
         Multi<Person> heroes() {
             return Multi.createFrom().items(
                     new Person("superman", 1),
                     new Person("batman", 2),
                     new Person("spiderman", 3));
         }
         
         
        NDJSON stands for Newline Delimited JSON. NDJSON is a convenient format for storing or streaming structured data that may be processed one record at a time:
        1. Line Separator is '\n',
        2. Each Line is a valid JSON value.
        See Also:
        Constant Field Values
    • Method Detail

      • asEventStream

        @Deprecated
        public static <T> io.smallrye.mutiny.Multi<T> asEventStream​(io.smallrye.mutiny.Multi<T> multi)
        Deprecated.
        Instead, set the `produces` attribute of the Route annotation to EVENT_STREAM and return a plain Multi.
        Indicates that 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

        @Deprecated
        public static <T> io.smallrye.mutiny.Multi<T> asJsonStream​(io.smallrye.mutiny.Multi<T> multi)
        Deprecated.
        Instead, set the `produces` attribute of the Route annotation to ND_JSON and return a plain Multi.
        Indicates that 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 a 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

        @Deprecated
        public static <T> io.smallrye.mutiny.Multi<T> asJsonArray​(io.smallrye.mutiny.Multi<T> multi)
        Deprecated.
        Instead, set the `produces` attribute of the Route annotation to APPLICATION_JSON and return a plain Multi.
        Indicates that 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