Class RequestHandler
- Direct Known Subclasses:
BaseViewHandler,SimpleHandler
Declare the route with @Route or a shorthand such as
@GET("/path"), then register via FlashRegistrar.scan(java.lang.String).
For ad-hoc routes use the fluent API (app.get(path, handler) or with middleware).
Lifecycle
- Instantiation — no-arg constructor (for scan) or manual
new Handler(...) bind(dev.relism.flash.extension.FlashContext)— called once by the framework atstart(), injects theFlashContextand invokesonInit()handle(dev.relism.flash.models.Request, dev.relism.flash.models.Response)— called on every matching request (hot-path, zero-alloc)
Service access
OverrideonInit() to cache services from the FlashContext
into private fields. This keeps the hot-path (handle) free of map lookups.
@GET("/users")
public class UserHandler extends RequestHandler {
private UserService users;
@Override protected void onInit() {
users = require(UserService.class);
}
@Override public Object handle(Request req, Response res) {
return users.findAll();
}
}
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidbind(FlashContext ctx) Called once by the framework after instantiation, before the first request.protected <T> Optional<T> Looks up an optional service from theFlashContext.abstract ObjectHandles an incoming request.protected voidonInit()Override to cache services at boot time.protected <T> Optional<T> Looks up an optional service from theFlashContext.protected <T> TRetrieves a required service from theFlashContext.
-
Constructor Details
-
RequestHandler
public RequestHandler()
-
-
Method Details
-
bind
Called once by the framework after instantiation, before the first request. Injects theFlashContextand triggersonInit().Infrastructure method — do not call from user code. Use
FlashRegistrar.scan(java.lang.String)instead. -
onInit
protected void onInit()Override to cache services at boot time. Called once afterbind(dev.relism.flash.extension.FlashContext), before any request reaches this handler.Use
require(java.lang.Class<T>)andfind(java.lang.Class<T>)to retrieve services from theFlashContext. Cache them in private fields so the hot-path (handle(dev.relism.flash.models.Request, dev.relism.flash.models.Response)) has zero lookup overhead.Important: if your class extends another handler base (e.g.
JacksonHandler), callsuper.onInit()first so the parent can initialise its own services.@Override protected void onInit() { super.onInit(); myService = require(MyService.class); } -
require
Retrieves a required service from theFlashContext. ThrowsIllegalStateExceptionif the service is not registered or this handler has not been bound yet.Typically called inside
onInit()to cache the result.- Type Parameters:
T- the service type- Parameters:
type- the service class- Returns:
- the service instance, never null
-
find
Looks up an optional service from theFlashContext.- Type Parameters:
T- the service type- Parameters:
type- the service class- Returns:
- the service, or empty if not registered
-
optional
Looks up an optional service from theFlashContext. Identical tofind(java.lang.Class<T>)— prefer this name for expressive call sites (optional(MyService.class).ifPresent(...)).- Type Parameters:
T- the service type- Parameters:
type- the service class- Returns:
- the service, or empty if not registered
-
handle
Handles an incoming request. The return value determines the response body: return aResponseto replace the whole response, any other non-null value to set it as the body, ornullto leave the response as-is.- Throws:
Exception
-