public class ReactiveRoutes extends Object
text/event-stream responses.| Modifier and Type | Class and Description |
|---|---|
static interface |
ReactiveRoutes.ServerSentEvent<T>
A class allowing to customized how the server sent events are written.
|
| Modifier and Type | Method and 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.
|
public static <T> io.smallrye.mutiny.Multi<T> asEventStream(io.smallrye.mutiny.Multi<T> multi)
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)));
}
T - the type of item, can be string, buffer, object or io.quarkus.vertx.web.ReactiveRoutes.ServerSentEventmulti - the multi to be writtenpublic static <T> io.smallrye.mutiny.Multi<T> asJsonArray(io.smallrye.mutiny.Multi<T> multi)
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}, {...}, {..,}]T - the type of item, can be string or objectmulti - the multi to be writtenCopyright © 2021 JBoss by Red Hat. All rights reserved.