Class ReactiveRoutes
- java.lang.Object
-
- io.quarkus.vertx.web.ReactiveRoutes
-
public class ReactiveRoutes extends Object
Provides utility methods, mainly to handletext/event-stream
responses.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ReactiveRoutes.ServerSentEvent<T>
A class allowing to customize how the server sent events are written.
-
Field Summary
Fields Modifier and Type Field Description static String
APPLICATION_JSON
The content-type to use to indicate you want to produce an JSON Array response, such as in:static String
EVENT_STREAM
The content-type to use to indicate you want to produce a server-sent-event (SSE) stream response, such as in:static String
JSON_STREAM
A content-type providing the same output asND_JSON
.static String
ND_JSON
The content-type to use to indicate you want to produce NDJSON stream response, such as in:
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <T> io.smallrye.mutiny.Multi<T>
asEventStream(io.smallrye.mutiny.Multi<T> multi)
Deprecated.Instead, set the `produces` attribute of theRoute
annotation toEVENT_STREAM
and return a plain Multi.static <T> io.smallrye.mutiny.Multi<T>
asJsonArray(io.smallrye.mutiny.Multi<T> multi)
Deprecated.Instead, set the `produces` attribute of theRoute
annotation toAPPLICATION_JSON
and return a plain Multi.static <T> io.smallrye.mutiny.Multi<T>
asJsonStream(io.smallrye.mutiny.Multi<T> multi)
-
-
-
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)); }
- 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:@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:@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)); }
- Line Separator is '\n',
- Each Line is a valid JSON value.
- See Also:
- Constant Field Values
-
JSON_STREAM
public static final String JSON_STREAM
A content-type providing the same output asND_JSON
.- 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 theRoute
annotation toEVENT_STREAM
and return a plain Multi.Indicates that the given stream should be written as server-sent-event in the response. Returning amulti
wrapped using this method produces atext/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. Anid
is automatically generated. If the item is a Buffer, thedata
part of the event is this buffer. Anid
is automatically generated. If the item is an Object, thedata
part of the event is the JSON representation of this object. Anid
is automatically generated. If the item is anReactiveRoutes.ServerSentEvent
, thedata
part of the event is the JSON representation of thisReactiveRoutes.ServerSentEvent.data()
. Theid
is computed fromReactiveRoutes.ServerSentEvent.id()
(generated if not implemented). Theevent
section (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
@Deprecated public static <T> io.smallrye.mutiny.Multi<T> asJsonStream(io.smallrye.mutiny.Multi<T> multi)
Deprecated.Indicates that the given stream should be written as a Json stream in the response. Returning amulti
wrapped using this method produces aapplication/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 theRoute
annotation toAPPLICATION_JSON
and return a plain Multi.Indicates that the given stream should be written as a chunked JSON array in the response. Returning amulti
wrapped using this method produces aapplication/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
-
-