T
- The return type of the handler@FunctionalInterface public interface SyncHandler<T>
Response
when you want to do things like modify response headers based on
results of service invocations or control the status code returned. Examples:
public Response invoke(RequestContext requestContext) {
String s = stringResponse();
return Response.forPayload(s)
.withHeader("X-Payload-Length", s.length());
}
public Response invoke(RequestContext requestContext) {
Map args = requestContext.pathArgs();
if (args.get("arg") == null || args.get("arg").isEmpty()) {
return Response.forStatus(Status.BAD_REQUEST
.withReasonPhrase("Mandatory argument 'arg' missing"));
}
return Response.forPayload(s);
}
In order to construct a Route
that can be registered with
Environment.RoutingEngine.registerRoute(Route)
, the return type of the handler must be a
Response<ByteString>
. This return type can be composed through your serialization
functions using map(Function)
and flatMap(Function)
.Modifier and Type | Method and Description |
---|---|
default <V> SyncHandler<V> |
flatMap(Function<? super T,? extends SyncHandler<? extends V>> mapFunction)
Create a new
SyncHandler that will map the return value of
invoke(RequestContext) through the given map function. |
T |
invoke(RequestContext requestContext) |
default <V> SyncHandler<V> |
map(Function<? super T,? extends V> mapFunction)
Create a new
SyncHandler that will map the return value of
invoke(RequestContext) through the given map function. |
T invoke(RequestContext requestContext)
default <V> SyncHandler<V> map(Function<? super T,? extends V> mapFunction)
SyncHandler
that will map the return value of
invoke(RequestContext)
through the given map function.V
- The resulting handler typemapFunction
- The mapping functionSyncHandler
with a composed invoke methoddefault <V> SyncHandler<V> flatMap(Function<? super T,? extends SyncHandler<? extends V>> mapFunction)
SyncHandler
that will map the return value of
invoke(RequestContext)
through the given map function.
The returned SyncHandler
of the map function will execute with
the same RequestContext
as the current handler.V
- The resulting handler typemapFunction
- The mapping functionSyncHandler
with a composed invoke methodCopyright © 2016. All rights reserved.