Class ReactiveRoutes
- java.lang.Object
-
- io.quarkus.vertx.web.ReactiveRoutes
-
public class ReactiveRoutes extends Object
Provides utility methods, mainly to handletext/event-streamresponses.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceReactiveRoutes.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 amultiwrapped using this method produces atext/event-streamresponse. 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
datapart of the event is this string. Anidis automatically generated. If the item is a Buffer, thedatapart of the event is this buffer. Anidis automatically generated. If the item is an Object, thedatapart of the event is the JSON representation of this object. Anidis automatically generated. If the item is anReactiveRoutes.ServerSentEvent, thedatapart of the event is the JSON representation of thisReactiveRoutes.ServerSentEvent.data(). Theidis computed fromReactiveRoutes.ServerSentEvent.id()(generated if not implemented). Theeventsection (ignored in all the other case) is computed fromReactiveRoutes.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 amultiwrapped using this method produces aapplication/x-ndjsonresponse. 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 amultiwrapped using this method produces aapplication/jsonresponse. 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
-
-