public interface Router
Modifier and Type | Method and Description |
---|---|
default Route.Definition |
after(Route.After handler)
after
|
default Route.Collection |
after(Route.After handler,
Route.After... next)
after
|
default Route.Definition |
after(String pattern,
Route.After handler)
after
|
default Route.Collection |
after(String pattern,
Route.After handler,
Route.After... next)
after
|
Route.Definition |
after(String method,
String pattern,
Route.After handler)
after
|
default Route.Collection |
after(String method,
String pattern,
Route.After handler,
Route.After... next)
after
|
default Route.AssetDefinition |
assets(String path)
Static files handler.
|
Route.AssetDefinition |
assets(String path,
AssetHandler handler)
Send static files, like
assets(String) but let you specify a custom
AssetHandler . |
Route.AssetDefinition |
assets(String path,
Path basedir)
Static files handler on external location.
|
Route.AssetDefinition |
assets(String path,
String location)
Static files handler.
|
default Route.Definition |
before(Route.Before handler)
before
|
default Route.Collection |
before(Route.Before handler,
Route.Before... next)
before
|
default Route.Definition |
before(String pattern,
Route.Before handler)
before
|
default Route.Collection |
before(String pattern,
Route.Before handler,
Route.Before... next)
before
|
Route.Definition |
before(String method,
String pattern,
Route.Before handler)
before
|
default Route.Collection |
before(String method,
String pattern,
Route.Before handler,
Route.Before... next)
before
|
default Route.Definition |
complete(Route.Complete handler)
complete
|
default Route.Collection |
complete(Route.Complete handler,
Route.Complete... next)
complete
|
default Route.Definition |
complete(String pattern,
Route.Complete handler)
complete
|
default Route.Collection |
complete(String pattern,
Route.Complete handler,
Route.Complete... next)
complete
|
Route.Definition |
complete(String method,
String pattern,
Route.Complete handler)
complete
|
default Route.Collection |
complete(String method,
String pattern,
Route.Complete handler,
Route.Complete... next)
complete
|
Route.Definition |
connect(String path,
Route.Filter filter)
Append a route that supports HTTP CONNECT method:
|
Route.Definition |
connect(String path,
Route.Handler handler)
Append a route that supports HTTP CONNECT method:
|
Route.Definition |
connect(String path,
Route.OneArgHandler handler)
Append route that supports HTTP CONNECT method:
|
Route.Definition |
connect(String path,
Route.ZeroArgHandler handler)
Append route that supports HTTP CONNECT method:
|
static String |
decode(String path)
Decode a path by delegating to
URLDecoder.decode(String, String) . |
default Route.ZeroArgHandler |
deferred(Route.OneArgHandler handler)
Functional version of
promise(org.jooby.Deferred.Initializer) . |
default Route.ZeroArgHandler |
deferred(Route.ZeroArgHandler handler)
Functional version of
promise(org.jooby.Deferred.Initializer) . |
default Route.ZeroArgHandler |
deferred(String executor,
Route.OneArgHandler handler)
Functional version of
promise(org.jooby.Deferred.Initializer) . |
default Route.ZeroArgHandler |
deferred(String executor,
Route.ZeroArgHandler handler)
Functional version of
promise(org.jooby.Deferred.Initializer) . |
default Route.Definition |
delete(Route.Handler handler)
Append a route that supports HTTP DELETE method:
|
default Route.Definition |
delete(Route.OneArgHandler handler)
Append route that supports HTTP DELETE method:
|
default Route.Definition |
delete(Route.ZeroArgHandler handler)
Append route that supports HTTP DELETE method:
|
Route.Definition |
delete(String path,
Route.Filter filter)
Append a route that supports HTTP DELETE method:
|
Route.Definition |
delete(String path,
Route.Handler handler)
Append a route that supports HTTP DELETE method:
|
Route.Definition |
delete(String path,
Route.OneArgHandler handler)
Append route that supports HTTP DELETE method:
|
Route.Definition |
delete(String path,
Route.ZeroArgHandler handler)
Append route that supports HTTP DELETE method:
|
Route.Collection |
delete(String path1,
String path2,
Route.Filter filter)
Append three routes that supports HTTP DELETE method on the same handler:
|
Route.Collection |
delete(String path1,
String path2,
Route.Handler handler)
Append three routes that supports HTTP DELETE method on the same handler:
|
Route.Collection |
delete(String path1,
String path2,
Route.OneArgHandler handler)
Append three routes that supports HTTP DELETE method on the same handler:
|
Route.Collection |
delete(String path1,
String path2,
Route.ZeroArgHandler handler)
Append three routes that supports HTTP DELETE method on the same handler:
|
Route.Collection |
delete(String path1,
String path2,
String path3,
Route.Filter filter)
Append three routes that supports HTTP DELETE method on the same handler:
|
Route.Collection |
delete(String path1,
String path2,
String path3,
Route.Handler handler)
Append three routes that supports HTTP DELETE method on the same handler:
|
Route.Collection |
delete(String path1,
String path2,
String path3,
Route.OneArgHandler handler)
Append three routes that supports HTTP DELETE method on the same handler:
|
Route.Collection |
delete(String path1,
String path2,
String path3,
Route.ZeroArgHandler handler)
Append three routes that supports HTTP DELETE method on the same handler:
|
default Router |
err(Class<? extends Throwable> type,
Err.Handler handler)
Setup a custom error handler.The error handler will be executed if the current exception is an
instance of given type type.
|
Router |
err(Err.Handler err)
Setup a route error handler.
|
default Router |
err(int statusCode,
Err.Handler handler)
Setup a route error handler.
|
default Router |
err(Predicate<Status> predicate,
Err.Handler handler)
Setup a route error handler.
|
default Router |
err(Status code,
Err.Handler handler)
Setup a route error handler.
|
default Route.Definition |
get(Route.Handler handler)
Append a route that matches the HTTP GET method:
|
default Route.Definition |
get(Route.OneArgHandler handler)
Append route that matches the HTTP GET method:
|
default Route.Definition |
get(Route.ZeroArgHandler handler)
Append route that matches HTTP GET method:
|
Route.Definition |
get(String path,
Route.Filter filter)
Append a filter that matches HTTP GET method:
|
Route.Definition |
get(String path,
Route.Handler handler)
Append a route that matches the HTTP GET method:
|
Route.Definition |
get(String path,
Route.OneArgHandler handler)
Append route that matches the HTTP GET method:
|
Route.Definition |
get(String path,
Route.ZeroArgHandler handler)
Append route that matches HTTP GET method:
|
Route.Collection |
get(String path1,
String path2,
Route.Filter filter)
Append three routes that matches the HTTP GET method on the same handler:
|
Route.Collection |
get(String path1,
String path2,
Route.Handler handler)
Append two routes that matches the HTTP GET method on the same handler:
|
Route.Collection |
get(String path1,
String path2,
Route.OneArgHandler handler)
Append three routes that matches the HTTP GET method on the same handler:
|
Route.Collection |
get(String path1,
String path2,
Route.ZeroArgHandler handler)
Append three routes that matches the HTTP GET method on the same handler:
|
Route.Collection |
get(String path1,
String path2,
String path3,
Route.Filter filter)
Append three routes that supports HTTP GET method on the same handler:
|
Route.Collection |
get(String path1,
String path2,
String path3,
Route.Handler handler)
Append three routes that matches the HTTP GET method on the same handler:
|
Route.Collection |
get(String path1,
String path2,
String path3,
Route.OneArgHandler handler)
Append three routes that matches the HTTP GET method on the same handler:
|
Route.Collection |
get(String path1,
String path2,
String path3,
Route.ZeroArgHandler handler)
Append three routes that matches HTTP GET method on the same handler:
|
Route.Definition |
head()
Append a new route that automatically handles HEAD request from existing GET routes.
|
Route.Definition |
head(String path,
Route.Filter filter)
Append a route that supports HTTP HEAD method:
|
Route.Definition |
head(String path,
Route.Handler handler)
Append a route that supports HTTP HEAD method:
|
Route.Definition |
head(String path,
Route.OneArgHandler handler)
Append route that supports HTTP HEAD method:
|
Route.Definition |
head(String path,
Route.ZeroArgHandler handler)
Append route that supports HTTP HEAD method:
|
Router |
map(Route.Mapper<?> mapper)
Apply the mapper to all the functional routes.
|
Route.Definition |
options()
Append a new route that automatically handles OPTIONS requests.
|
Route.Definition |
options(String path,
Route.Filter filter)
Append a route that supports HTTP OPTIONS method:
|
Route.Definition |
options(String path,
Route.Handler handler)
Append a route that supports HTTP OPTIONS method:
|
Route.Definition |
options(String path,
Route.OneArgHandler handler)
Append route that supports HTTP OPTIONS method:
|
Route.Definition |
options(String path,
Route.ZeroArgHandler handler)
Append route that supports HTTP OPTIONS method:
|
default Route.Definition |
patch(Route.Handler handler)
Append route that supports HTTP PATCH method:
|
default Route.Definition |
patch(Route.OneArgHandler handler)
Append route that supports HTTP PATCH method:
|
default Route.Definition |
patch(Route.ZeroArgHandler handler)
Append route that supports HTTP PATCH method:
|
Route.Definition |
patch(String path,
Route.Filter filter)
Append route that supports HTTP PATCH method:
|
Route.Definition |
patch(String path,
Route.Handler handler)
Append route that supports HTTP PATCH method:
|
Route.Definition |
patch(String path,
Route.OneArgHandler handler)
Append route that supports HTTP PATCH method:
|
Route.Definition |
patch(String path,
Route.ZeroArgHandler handler)
Append route that supports HTTP PATCH method:
|
Route.Collection |
patch(String path1,
String path2,
Route.Filter filter)
Append three routes that supports HTTP PATCH method on the same handler:
|
Route.Collection |
patch(String path1,
String path2,
Route.Handler handler)
Append three routes that supports HTTP PATCH method on the same handler:
|
Route.Collection |
patch(String path1,
String path2,
Route.OneArgHandler handler)
Append three routes that supports HTTP PATCH method on the same handler:
|
Route.Collection |
patch(String path1,
String path2,
Route.ZeroArgHandler handler)
Append three routes that supports HTTP PATCH method on the same handler:
|
Route.Collection |
patch(String path1,
String path2,
String path3,
Route.Filter filter)
Append three routes that supports HTTP PATCH method on the same handler:
|
Route.Collection |
patch(String path1,
String path2,
String path3,
Route.Handler handler)
Append three routes that supports HTTP PATCH method on the same handler:
|
Route.Collection |
patch(String path1,
String path2,
String path3,
Route.OneArgHandler handler)
Append three routes that supports HTTP PATCH method on the same handler:
|
Route.Collection |
patch(String path1,
String path2,
String path3,
Route.ZeroArgHandler handler)
Append three routes that supports HTTP PATCH method on the same handler:
|
Route.Collection |
path(String path,
Runnable action)
Group one or more routes under a common path.
|
default Route.Definition |
post(Route.Handler handler)
Append a route that supports HTTP POST method:
|
default Route.Definition |
post(Route.OneArgHandler handler)
Append route that supports HTTP POST method:
|
default Route.Definition |
post(Route.ZeroArgHandler handler)
Append route that supports HTTP POST method:
|
Route.Definition |
post(String path,
Route.Filter filter)
Append a route that supports HTTP POST method:
|
Route.Definition |
post(String path,
Route.Handler handler)
Append a route that supports HTTP POST method:
|
Route.Definition |
post(String path,
Route.OneArgHandler handler)
Append route that supports HTTP POST method:
|
Route.Definition |
post(String path,
Route.ZeroArgHandler handler)
Append route that supports HTTP POST method:
|
Route.Collection |
post(String path1,
String path2,
Route.Filter filter)
Append three routes that supports HTTP POST method on the same handler:
|
Route.Collection |
post(String path1,
String path2,
Route.Handler handler)
Append three routes that supports HTTP POST method on the same handler:
|
Route.Collection |
post(String path1,
String path2,
Route.OneArgHandler handler)
Append three routes that supports HTTP POST method on the same handler:
|
Route.Collection |
post(String path1,
String path2,
Route.ZeroArgHandler handler)
Append three routes that supports HTTP POST method on the same handler:
|
Route.Collection |
post(String path1,
String path2,
String path3,
Route.Filter filter)
Append three routes that supports HTTP POST method on the same handler:
|
Route.Collection |
post(String path1,
String path2,
String path3,
Route.Handler handler)
Append three routes that supports HTTP POST method on the same handler:
|
Route.Collection |
post(String path1,
String path2,
String path3,
Route.OneArgHandler handler)
Append three routes that supports HTTP POST method on the same handler:
|
Route.Collection |
post(String path1,
String path2,
String path3,
Route.ZeroArgHandler handler)
Append three routes that supports HTTP POST method on the same handler:
|
Route.OneArgHandler |
promise(Deferred.Initializer initializer)
Produces a deferred response, useful for async request processing.
|
Route.OneArgHandler |
promise(Deferred.Initializer0 initializer)
Produces a deferred response, useful for async request processing.
|
Route.OneArgHandler |
promise(String executor,
Deferred.Initializer initializer)
Produces a deferred response, useful for async request processing.
|
Route.OneArgHandler |
promise(String executor,
Deferred.Initializer0 initializer)
Produces a deferred response, useful for async request processing.
|
default Route.Definition |
put(Route.Handler handler)
Append route that supports HTTP PUT method:
|
default Route.Definition |
put(Route.OneArgHandler handler)
Append route that supports HTTP PUT method:
|
default Route.Definition |
put(Route.ZeroArgHandler handler)
Append route that supports HTTP PUT method:
|
Route.Definition |
put(String path,
Route.Filter filter)
Append route that supports HTTP PUT method:
|
Route.Definition |
put(String path,
Route.Handler handler)
Append route that supports HTTP PUT method:
|
Route.Definition |
put(String path,
Route.OneArgHandler handler)
Append route that supports HTTP PUT method:
|
Route.Definition |
put(String path,
Route.ZeroArgHandler handler)
Append route that supports HTTP PUT method:
|
Route.Collection |
put(String path1,
String path2,
Route.Filter filter)
Append three routes that supports HTTP PUT method on the same handler:
|
Route.Collection |
put(String path1,
String path2,
Route.Handler handler)
Append three routes that supports HTTP PUT method on the same handler:
|
Route.Collection |
put(String path1,
String path2,
Route.OneArgHandler handler)
Append three routes that supports HTTP PUT method on the same handler:
|
Route.Collection |
put(String path1,
String path2,
Route.ZeroArgHandler handler)
Append three routes that supports HTTP PUT method on the same handler:
|
Route.Collection |
put(String path1,
String path2,
String path3,
Route.Filter filter)
Append three routes that supports HTTP PUT method on the same handler:
|
Route.Collection |
put(String path1,
String path2,
String path3,
Route.Handler handler)
Append three routes that supports HTTP PUT method on the same handler:
|
Route.Collection |
put(String path1,
String path2,
String path3,
Route.OneArgHandler handler)
Append three routes that supports HTTP PUT method on the same handler:
|
Route.Collection |
put(String path1,
String path2,
String path3,
Route.ZeroArgHandler handler)
Append three routes that supports HTTP PUT method on the same handler:
|
Route.Definition |
sse(String path,
Sse.Handler handler)
Add a server-sent event handler.
|
Route.Definition |
sse(String path,
Sse.Handler1 handler)
Add a server-sent event handler.
|
Route.Definition |
trace()
Append a default trace implementation under the given path.
|
Route.Definition |
trace(String path,
Route.Filter filter)
Append a route that supports HTTP TRACE method:
|
Route.Definition |
trace(String path,
Route.Handler handler)
Append a route that supports HTTP TRACE method:
|
Route.Definition |
trace(String path,
Route.OneArgHandler handler)
Append route that supports HTTP TRACE method:
|
Route.Definition |
trace(String path,
Route.ZeroArgHandler handler)
Append route that supports HTTP TRACE method:
|
Route.Collection |
use(Class<?> routeClass)
Append MVC routes from a controller like class:
|
Router |
use(Jooby app)
Import content from provide application (routes, parsers/renderers, start/stop callbacks, ...
|
Route.Collection |
use(String path,
Class<?> routeClass)
Append MVC routes from a controller like class:
|
Router |
use(String path,
Jooby app)
Import content from provide application (routes, parsers/renderers, start/stop callbacks, ...
|
Route.Definition |
use(String path,
Route.Filter filter)
Append a new filter that matches any method under the given path.
|
Route.Definition |
use(String path,
Route.Handler handler)
Append a new route handler that matches any method under the given path.
|
Route.Definition |
use(String path,
Route.OneArgHandler handler)
Append a new route handler that matches any method under the given path.
|
Route.Definition |
use(String method,
String path,
Route.Filter filter)
Append a new filter that matches the given method and path.
|
Route.Definition |
use(String method,
String path,
Route.Handler handler)
Append a new route handler that matches the given method and path.
|
Route.Collection |
with(Runnable callback)
Apply common configuration and attributes to a group of routes:
|
default <T> WebSocket.Definition |
ws(Class<? extends WebSocket.OnMessage<T>> handler)
Append a new WebSocket handler under the given path.
|
<T> WebSocket.Definition |
ws(String path,
Class<? extends WebSocket.OnMessage<T>> handler)
Append a new WebSocket handler under the given path.
|
WebSocket.Definition |
ws(String path,
WebSocket.OnOpen handler)
Append a new WebSocket handler under the given path.
|
default WebSocket.Definition |
ws(String path,
WebSocket.OnOpen1 handler)
Append a new WebSocket handler under the given path.
|
static String decode(String path)
URLDecoder.decode(String, String)
.path
- Path to decoded.URLDecoder.decode(String, String)
.@Nonnull Router use(Jooby app)
app
- Routes provider.Route.Collection path(String path, Runnable action)
{
path("/api/pets", () -> {
});
}
path
- Common path.action
- Router action.@Nonnull Router use(String path, Jooby app)
path
- Path to mount the given app.app
- Routes provider.@Nonnull Route.Definition use(String path, Route.Filter filter)
path
- A path pattern.filter
- A filter to execute.@Nonnull Route.Definition use(String method, String path, Route.Filter filter)
method
- A HTTP method.path
- A path pattern.filter
- A filter to execute.@Nonnull Route.Definition use(String method, String path, Route.Handler handler)
use(String, String, org.jooby.Route.Filter)
but you don't have to explicitly call
Route.Chain.next(Request, Response)
.method
- A HTTP method.path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition use(String path, Route.Handler handler)
use(String, org.jooby.Route.Filter)
but you don't have to explicitly call
Route.Chain.next(Request, Response)
.path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition use(String path, Route.OneArgHandler handler)
path
- A path pattern.handler
- A handler to execute.@Nonnull default Route.Definition get(Route.Handler handler)
get((req, rsp) -> { rsp.send(something); });
handler
- A handler to execute.@Nonnull Route.Definition get(String path, Route.Handler handler)
get("/", (req, rsp) -> { rsp.send(something); });
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection get(String path1, String path2, Route.Handler handler)
get("/model", "/mode/:id", (req, rsp) -> { rsp.send(req.param("id").toOptional(String.class)); });
path1
- A path pattern.path2
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection get(String path1, String path2, String path3, Route.Handler handler)
get("/p1", "/p2", "/p3", (req, rsp) -> { rsp.send(req.path()); });
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.handler
- A handler to execute.@Nonnull default Route.Definition get(Route.OneArgHandler handler)
get(req -> { return "hello"; });
handler
- A handler to execute.@Nonnull Route.Definition get(String path, Route.OneArgHandler handler)
get("/", req -> { return "hello"; });
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection get(String path1, String path2, Route.OneArgHandler handler)
get("/model", "/model/:id", req -> { return req.param("id").toOptional(String.class); });
path1
- A path pattern.path2
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection get(String path1, String path2, String path3, Route.OneArgHandler handler)
get("/p1", "/p2", "/p3", req -> { return req.path(); });
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.handler
- A handler to execute.@Nonnull default Route.Definition get(Route.ZeroArgHandler handler)
get(() -> "hello" );
handler
- A handler to execute.@Nonnull Route.Definition get(String path, Route.ZeroArgHandler handler)
get("/", () -> "hello" );
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection get(String path1, String path2, Route.ZeroArgHandler handler)
get("/p1", "/p2", () -> { return "OK"; });
path1
- A path pattern.path2
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection get(String path1, String path2, String path3, Route.ZeroArgHandler handler)
get("/p1", "/p2", "/p3", () -> { return "OK"; });
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition get(String path, Route.Filter filter)
get("/", (req, rsp, chain) -> { chain.next(req, rsp); });
path
- A path pattern.filter
- A filter to execute.@Nonnull Route.Collection get(String path1, String path2, Route.Filter filter)
get("/model", "/model/:id", (req, rsp, chain) -> { req.param("id").toOptional(String.class); chain.next(req, rsp); });
path1
- A path pattern.path2
- A path pattern.filter
- A filter to execute.@Nonnull Route.Collection get(String path1, String path2, String path3, Route.Filter filter)
get("/p1", "/p2", "/p3", (req, rsp, chain) -> { chain.next(req, rsp); });
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.filter
- A filter to execute.@Nonnull default Route.Definition post(Route.Handler handler)
post((req, rsp) -> { rsp.send(something); });
handler
- A handler to execute.@Nonnull Route.Definition post(String path, Route.Handler handler)
post("/", (req, rsp) -> { rsp.send(something); });
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection post(String path1, String path2, Route.Handler handler)
post("/p1", "/p2", (req, rsp) -> { rsp.send(req.path()); });
path1
- A path pattern.path2
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection post(String path1, String path2, String path3, Route.Handler handler)
post("/p1", "/p2", "/p3", (req, rsp) -> { rsp.send(req.path()); });
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.handler
- A handler to execute.@Nonnull default Route.Definition post(Route.OneArgHandler handler)
post(req -> "hello" );
handler
- A handler to execute.@Nonnull Route.Definition post(String path, Route.OneArgHandler handler)
post("/", req -> "hello" );
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection post(String path1, String path2, Route.OneArgHandler handler)
post("/p1", "/p2", req -> { return req.path(); });
path1
- A path pattern.path2
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection post(String path1, String path2, String path3, Route.OneArgHandler handler)
post("/p1", "/p2", "/p3", req -> { return req.path(); });
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.handler
- A handler to execute.@Nonnull default Route.Definition post(Route.ZeroArgHandler handler)
post(() -> "hello" );
handler
- A handler to execute.@Nonnull Route.Definition post(String path, Route.ZeroArgHandler handler)
post("/", () -> "hello" );
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection post(String path1, String path2, Route.ZeroArgHandler handler)
post("/p1", "/p2", -> { return "OK"; });
path1
- A path pattern.path2
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection post(String path1, String path2, String path3, Route.ZeroArgHandler handler)
post("/p1", "/p2", "/p3", () -> { return "OK"; });
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition post(String path, Route.Filter filter)
post("/", (req, rsp, chain) -> { chain.next(req, rsp); });
path
- A path pattern.filter
- A filter to execute.@Nonnull Route.Collection post(String path1, String path2, Route.Filter filter)
post("/p1", "/p2",(req, rsp, chain) -> { chain.next(req, rsp); });
path1
- A path pattern.path2
- A path pattern.filter
- A filter to execute.@Nonnull Route.Collection post(String path1, String path2, String path3, Route.Filter filter)
post("/p1", "/p2", "/p3", (req, rsp, chain) -> { chain.next(req, rsp); });
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.filter
- A filter to execute.@Nonnull Route.Definition head(String path, Route.Handler handler)
post("/", (req, rsp) -> { rsp.send(something); });
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition head(String path, Route.OneArgHandler handler)
head("/", req -> "hello" );
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition head(String path, Route.ZeroArgHandler handler)
head("/", () -> "hello" );
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition head(String path, Route.Filter filter)
post("/", (req, rsp, chain) -> { chain.next(req, rsp); });
path
- A path pattern.filter
- A filter to execute.@Nonnull Route.Definition head()
{ head(); }
@Nonnull Route.Definition options(String path, Route.Handler handler)
options("/", (req, rsp) -> { rsp.header("Allow", "GET, POST"); });
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition options(String path, Route.OneArgHandler handler)
options("/", req -> return Results.with(200).header("Allow", "GET, POST") );
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition options(String path, Route.ZeroArgHandler handler)
options("/", () -> return Results.with(200).header("Allow", "GET, POST") );
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition options(String path, Route.Filter filter)
options("/", (req, rsp, chain) -> { rsp.header("Allow", "GET, POST"); chain.next(req, rsp); });
path
- A path pattern.filter
- A callback to execute.@Nonnull Route.Definition options()
get("/", (req, rsp) -> { rsp.send(something); }); post("/", (req, rsp) -> { rsp.send(something); }); options("/");
OPTIONS /
produces a response with a Allow header set to: GET, POST.@Nonnull default Route.Definition put(Route.Handler handler)
put((req, rsp) -> { rsp.send(something); });
handler
- A route to execute.@Nonnull Route.Definition put(String path, Route.Handler handler)
put("/", (req, rsp) -> { rsp.send(something); });
path
- A path pattern.handler
- A route to execute.@Nonnull Route.Collection put(String path1, String path2, Route.Handler handler)
put("/p1", "/p2", (req, rsp) -> { rsp.send(req.path()); });
path1
- A path pattern.path2
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection put(String path1, String path2, String path3, Route.Handler handler)
put("/p1", "/p2", "/p3", (req, rsp) -> { rsp.send(req.path()); });
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.handler
- A handler to execute.@Nonnull default Route.Definition put(Route.OneArgHandler handler)
put(req -> return Results.accepted(); );
handler
- A handler to execute.@Nonnull Route.Definition put(String path, Route.OneArgHandler handler)
put("/", req -> return Results.accepted(); );
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection put(String path1, String path2, Route.OneArgHandler handler)
put("/p1", "/p2", req -> { return req.path(); });
path1
- A path pattern.path2
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection put(String path1, String path2, String path3, Route.OneArgHandler handler)
put("/p1", "/p2", "/p3", req -> { return req.path(); });This is a singleton route so make sure you don't share or use global variables.
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.handler
- A handler to execute.@Nonnull default Route.Definition put(Route.ZeroArgHandler handler)
put(() -> { return Results.accepted() });
handler
- A handler to execute.@Nonnull Route.Definition put(String path, Route.ZeroArgHandler handler)
put("/", () -> { return Results.accepted() });
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection put(String path1, String path2, Route.ZeroArgHandler handler)
put("/p1", "/p2", req -> { return req.path(); });This is a singleton route so make sure you don't share or use global variables.
path1
- A path pattern.path2
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection put(String path1, String path2, String path3, Route.ZeroArgHandler handler)
put("/p1", "/p2", "/p3", req -> { return req.path(); });This is a singleton route so make sure you don't share or use global variables.
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition put(String path, Route.Filter filter)
put("/", (req, rsp, chain) -> { chain.next(req, rsp); });
path
- A path pattern.filter
- A callback to execute.@Nonnull Route.Collection put(String path1, String path2, Route.Filter filter)
put("/p1", "/p2", (req, rsp, chain) -> { chain.next(req, rsp); });
path1
- A path pattern.path2
- A path pattern.filter
- A filter to execute.@Nonnull Route.Collection put(String path1, String path2, String path3, Route.Filter filter)
put("/p1", "/p2", "/p3", (req, rsp, chain) -> { chain.next(req, rsp); });This is a singleton route so make sure you don't share or use global variables.
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.filter
- A filter to execute.@Nonnull default Route.Definition patch(Route.Handler handler)
patch((req, rsp) -> { rsp.send(something); });
handler
- A route to execute.@Nonnull Route.Definition patch(String path, Route.Handler handler)
patch("/", (req, rsp) -> { rsp.send(something); });
path
- A path pattern.handler
- A route to execute.@Nonnull Route.Collection patch(String path1, String path2, Route.Handler handler)
patch("/p1", "/p2", (req, rsp) -> { rsp.send(something); });
path1
- A path pattern.path2
- A path pattern.handler
- A handler to execute.Route.Collection patch(String path1, String path2, String path3, Route.Handler handler)
patch("/p1", "/p2", "/p3", (req, rsp) -> { rsp.send(something); });
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.handler
- A handler to execute.@Nonnull default Route.Definition patch(Route.OneArgHandler handler)
patch(req -> Results.ok() );This is a singleton route so make sure you don't share or use global variables.
handler
- A handler to execute.@Nonnull Route.Definition patch(String path, Route.OneArgHandler handler)
patch("/", req -> Results.ok() );This is a singleton route so make sure you don't share or use global variables.
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection patch(String path1, String path2, Route.OneArgHandler handler)
patch("/p1", "/p2", req -> { return req.path(); });This is a singleton route so make sure you don't share or use global variables.
path1
- A path pattern.path2
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection patch(String path1, String path2, String path3, Route.OneArgHandler handler)
patch("/p1", "/p2", "/p3", req -> { return req.path(); });This is a singleton route so make sure you don't share or use global variables.
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.handler
- A handler to execute.@Nonnull default Route.Definition patch(Route.ZeroArgHandler handler)
patch(() -> { return Results.ok(); });This is a singleton route so make sure you don't share or use global variables.
handler
- A handler to execute.@Nonnull Route.Definition patch(String path, Route.ZeroArgHandler handler)
patch("/", () -> { return Results.ok(); });This is a singleton route so make sure you don't share or use global variables.
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection patch(String path1, String path2, Route.ZeroArgHandler handler)
patch("/p1", "/p2", () -> { return Results.ok(); });This is a singleton route so make sure you don't share or use global variables.
path1
- A path pattern.path2
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection patch(String path1, String path2, String path3, Route.ZeroArgHandler handler)
patch("/p1", "/p2", "/p3", () -> { return Results.ok(); });This is a singleton route so make sure you don't share or use global variables.
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition patch(String path, Route.Filter filter)
patch("/", (req, rsp, chain) -> { chain.next(req, rsp); });
path
- A path pattern.filter
- A callback to execute.@Nonnull Route.Collection patch(String path1, String path2, Route.Filter filter)
patch("/p1", "/p2", (req, rsp, chain) -> { chain.next(req, rsp); });
path1
- A path pattern.path2
- A path pattern.filter
- A filter to execute.@Nonnull Route.Collection patch(String path1, String path2, String path3, Route.Filter filter)
patch("/p1", "/p2", "/p3", (req, rsp, chain) -> { chain.next(req, rsp); });
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.filter
- A filter to execute.@Nonnull default Route.Definition delete(Route.Handler handler)
delete((req, rsp) -> { rsp.status(204); });This is a singleton route so make sure you don't share or use global variables.
handler
- A handler to execute.@Nonnull Route.Definition delete(String path, Route.Handler handler)
delete("/", (req, rsp) -> { rsp.status(204); });This is a singleton route so make sure you don't share or use global variables.
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection delete(String path1, String path2, Route.Handler handler)
delete("/p1", "/p2", (req, rsp) -> { rsp.status(204); });This is a singleton route so make sure you don't share or use global variables.
path1
- A path pattern.path2
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection delete(String path1, String path2, String path3, Route.Handler handler)
delete("/p1", "/p2", "/p3", (req, rsp) -> { rsp.status(204); });This is a singleton route so make sure you don't share or use global variables.
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.handler
- A handler to execute.@Nonnull default Route.Definition delete(Route.OneArgHandler handler)
delete(req -> return Results.noContent(); );This is a singleton route so make sure you don't share or use global variables.
handler
- A handler to execute.@Nonnull Route.Definition delete(String path, Route.OneArgHandler handler)
delete("/", req -> return Results.noContent(); );This is a singleton route so make sure you don't share or use global variables.
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection delete(String path1, String path2, Route.OneArgHandler handler)
delete("/p1", "/p2", req -> { return Results.noContent(); });This is a singleton route so make sure you don't share or use global variables.
path1
- A path pattern.path2
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection delete(String path1, String path2, String path3, Route.OneArgHandler handler)
delete("/p1", "/p2", "/p3",req -> { return Results.noContent(); });This is a singleton route so make sure you don't share or use global variables.
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.handler
- A handler to execute.@Nonnull default Route.Definition delete(Route.ZeroArgHandler handler)
delete(() -> return Results.noContent(); );This is a singleton route so make sure you don't share or use global variables.
handler
- A handler to execute.@Nonnull Route.Definition delete(String path, Route.ZeroArgHandler handler)
delete("/", () -> return Results.noContent(); );This is a singleton route so make sure you don't share or use global variables.
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection delete(String path1, String path2, Route.ZeroArgHandler handler)
delete("/p1", "/p2", () -> { return Results.noContent(); });
path1
- A path pattern.path2
- A path pattern.handler
- A handler to execute.@Nonnull Route.Collection delete(String path1, String path2, String path3, Route.ZeroArgHandler handler)
delete("/p1", "/p2", "/p3", req -> { return Results.noContent(); });
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition delete(String path, Route.Filter filter)
delete("/", (req, rsp, chain) -> { rsp.status(304); chain.next(req, rsp); });
path
- A path pattern.filter
- A callback to execute.@Nonnull Route.Collection delete(String path1, String path2, Route.Filter filter)
delete("/p1", "/p2", (req, rsp, chain) -> { rsp.status(304); chain.next(req, rsp); });
path1
- A path pattern.path2
- A path pattern.filter
- A filter to execute.@Nonnull Route.Collection delete(String path1, String path2, String path3, Route.Filter filter)
delete("/p1", "/p2", "/p3", (req, rsp, chain) -> { rsp.status(304); chain.next(req, rsp); });
path1
- A path pattern.path2
- A path pattern.path3
- A path pattern.filter
- A filter to execute.@Nonnull Route.Definition trace(String path, Route.Handler handler)
trace("/", (req, rsp) -> { rsp.send(...); });
path
- A path pattern.handler
- A callback to execute.@Nonnull Route.Definition trace(String path, Route.OneArgHandler handler)
trace("/", req -> "trace" );
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition trace(String path, Route.ZeroArgHandler handler)
trace("/", () -> "trace" );
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition trace(String path, Route.Filter filter)
trace("/", (req, rsp, chain) -> { chain.next(req, rsp); });
path
- A path pattern.filter
- A callback to execute.@Nonnull Route.Definition trace()
TRACE /path header1: value header2: value
@Nonnull Route.Definition connect(String path, Route.Handler handler)
connect("/", (req, rsp) -> { });
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition connect(String path, Route.OneArgHandler handler)
connect("/", req -> "hello" );
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition connect(String path, Route.ZeroArgHandler handler)
connect("/", () -> "connected" );
path
- A path pattern.handler
- A handler to execute.@Nonnull Route.Definition connect(String path, Route.Filter filter)
connect("/", (req, rsp, chain) -> { chain.next(req, rsp); });
path
- A path pattern.filter
- A filter to execute.@Nonnull default Route.AssetDefinition assets(String path)
assets("/assets/**");Resources are served from root of classpath, for example
GET /assets/file.js
will
be resolve as classpath resource at the same location.
The AssetHandler
one step forward and add support for serving files from a CDN out of
the box. All you have to do is to define a assets.cdn
property:
assets.cdn = "http://d7471vfo50fqt.cloudfront.net"A GET to
/assets/js/index.js
will be redirected to:
http://d7471vfo50fqt.cloudfront.net/assets/js/index.js
.
You can turn on/off ETag
and Last-Modified
headers too using
assets.etag
and assets.lastModified
. These two properties are enabled
by default.path
- The path to publish.@Nonnull Route.AssetDefinition assets(String path, Path basedir)
assets("/assets/**", Paths.get("/www"));For example
GET /assets/file.js
will be resolve as /www/file.js
on
server file system.
The AssetHandler
one step forward and add support for serving files from a CDN out of
the box. All you have to do is to define a assets.cdn
property:
assets.cdn = "http://d7471vfo50fqt.cloudfront.net"A GET to
/assets/js/index.js
will be redirected to:
http://d7471vfo50fqt.cloudfront.net/assets/js/index.js
.
You can turn on/off ETag
and Last-Modified
headers too using
assets.etag
and assets.lastModified
. These two properties are enabled
by default.path
- The path to publish.basedir
- Base directory.@Nonnull Route.AssetDefinition assets(String path, String location)
assets(String)
but let you specify a different classpath
location.
Basic example
assets("/js/**", "/");A request for:
/js/jquery.js
will be translated to: /lib/jquery.js
.
Webjars example:
assets("/js/**", "/resources/webjars/{0}");A request for:
/js/jquery/2.1.3/jquery.js
will be translated to:
/resources/webjars/jquery/2.1.3/jquery.js
.
The {0}
represent the **
capturing group.
Another webjars example:
assets("/js/*-*.js", "/resources/webjars/{0}/{1}/{0}.js");
A request for: /js/jquery-2.1.3.js
will be translated to:
/resources/webjars/jquery/2.1.3/jquery.js
.
path
- The path to publish.location
- A resource location.@Nonnull Route.AssetDefinition assets(String path, AssetHandler handler)
assets(String)
but let you specify a custom
AssetHandler
.path
- The path to publish.handler
- Asset handler.@Nonnull Route.Collection use(Class<?> routeClass)
Append MVC routes from a controller like class:
use(MyRoute.class);Where MyRoute.java is:
@Path("/") public class MyRoute { @GET public String hello() { return "Hello Jooby"; } }
Programming model is quite similar to JAX-RS/Jersey with some minor differences and/or simplifications.
To learn more about Mvc Routes, please check Path
,
Produces
Consumes
.
routeClass
- A route(s) class.@Nonnull Route.Collection use(String path, Class<?> routeClass)
Append MVC routes from a controller like class:
use("/pets", MyRoute.class);Where MyRoute.java is:
@Path("/") public class MyRoute { @GET public String hello() { return "Hello Jooby"; } }
Programming model is quite similar to JAX-RS/Jersey with some minor differences and/or simplifications.
To learn more about Mvc Routes, please check Path
,
Produces
Consumes
.
path
- Path to mount the route.routeClass
- A route(s) class.@Nonnull default Route.Definition before(Route.Before handler)
{
before((req, rsp) -> {
// your code goes here
});
}
You are allowed to modify the request and response objects.
Please note that the before
handler is just syntax sugar for Route.Filter
.
For example, the before
handler was implemented as:
{
use("*", "*", (req, rsp, chain) -> {
before(req, rsp);
// your code goes here
chain.next(req, rsp);
});
}
A before
handler must to be registered before the actual handler you want to
intercept.
{
before((req, rsp) -> {
// your code goes here
});
get("/path", req -> {
// your code goes here
return ...;
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
handler
- Before handler.@Nonnull default Route.Collection before(Route.Before handler, Route.Before... next)
{
before((req, rsp) -> {
// your code goes here
});
}
You are allowed to modify the request and response objects.
Please note that the before
handler is just syntax sugar for Route.Filter
.
For example, the before
handler was implemented as:
{
use("*", "*", (req, rsp, chain) -> {
before(req, rsp);
// your code goes here
chain.next(req, rsp);
});
}
A before
handler must to be registered before the actual handler you want to
intercept.
{
before((req, rsp) -> {
// your code goes here
});
get("/path", req -> {
// your code goes here
return ...;
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
handler
- Before handler.next
- Next handler.@Nonnull default Route.Definition before(String pattern, Route.Before handler)
{
before("*", (req, rsp) -> {
// your code goes here
});
}
You are allowed to modify the request and response objects.
Please note that the before
handler is just syntax sugar for Route.Filter
.
For example, the before
handler was implemented as:
{
use("*", (req, rsp, chain) -> {
before(req, rsp);
chain.next(req, rsp);
});
}
A before
handler must to be registered before the actual handler you want to
intercept.
{
before("/path", (req, rsp) -> {
// your code goes here
});
get("/path", req -> {
// your code goes here
return ...;
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
pattern
- Pattern to intercept.handler
- Before handler.@Nonnull default Route.Collection before(String pattern, Route.Before handler, Route.Before... next)
{
before("*", (req, rsp) -> {
// your code goes here
});
}
You are allowed to modify the request and response objects.
Please note that the before
handler is just syntax sugar for Route.Filter
.
For example, the before
handler was implemented as:
{
use("*", (req, rsp, chain) -> {
before(req, rsp);
chain.next(req, rsp);
});
}
A before
handler must to be registered before the actual handler you want to
intercept.
{
before("/path", (req, rsp) -> {
// your code goes here
});
get("/path", req -> {
// your code goes here
return ...;
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
pattern
- Pattern to intercept.handler
- Before handler.next
- Next handler.@Nonnull Route.Definition before(String method, String pattern, Route.Before handler)
{
before("GET", "*", (req, rsp) -> {
// your code goes here
});
}
You are allowed to modify the request and response objects.
Please note that the before
handler is just syntax sugar for Route.Filter
.
For example, the before
handler was implemented as:
{
use("GET", "*", (req, rsp, chain) -> {
before(req, rsp);
chain.next(req, rsp);
});
}
A before
handler must to be registered before the actual handler you want to
intercept.
{
before("GET", "/path", (req, rsp) -> {
// your code goes here
});
get("/path", req -> {
// your code goes here
return ...;
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
method
- HTTP method to intercept.pattern
- Pattern to intercept.handler
- Before handler.@Nonnull default Route.Collection before(String method, String pattern, Route.Before handler, Route.Before... next)
{
before("GET", "*", (req, rsp) -> {
// your code goes here
});
}
You are allowed to modify the request and response objects.
Please note that the before
handler is just syntax sugar for Route.Filter
.
For example, the before
handler was implemented as:
{
use("GET", "*", (req, rsp, chain) -> {
before(req, rsp);
chain.next(req, rsp);
});
}
A before
handler must to be registered before the actual handler you want to
intercept.
{
before("GET", "/path", (req, rsp) -> {
// your code goes here
});
get("/path", req -> {
// your code goes here
return ...;
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
method
- HTTP method to intercept.pattern
- Pattern to intercept.handler
- Before handler.next
- Next handler.@Nonnull default Route.Definition after(Route.After handler)
{
after((req, rsp, result) -> {
// your code goes here
return result;
});
}
You are allowed to modify the request, response and result objects. The handler returns a
Result
which can be the same or an entirely new Result
.
A after
handler must to be registered before the actual handler you want to
intercept.
{
after((req, rsp, result) -> {
// your code goes here
return result;
});
get("/path", req -> {
return "hello";
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
handler
- After handler.@Nonnull default Route.Collection after(Route.After handler, Route.After... next)
{
after((req, rsp, result) -> {
// your code goes here
return result;
});
}
You are allowed to modify the request, response and result objects. The handler returns a
Result
which can be the same or an entirely new Result
.
A after
handler must to be registered before the actual handler you want to
intercept.
{
after((req, rsp, result) -> {
// your code goes here
return result;
});
get("/path", req -> {
return "hello";
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
handler
- After handler.next
- Next handler.@Nonnull default Route.Definition after(String pattern, Route.After handler)
{
after("*", (req, rsp, result) -> {
// your code goes here
return result;
});
}
You are allowed to modify the request, response and result objects. The handler returns a
Result
which can be the same or an entirely new Result
.
A after
handler must to be registered before the actual handler you want to
intercept.
{
after("/path", (req, rsp, result) -> {
// your code goes here
return result;
});
get("/path", req -> {
return "hello";
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
pattern
- Pattern to intercept.handler
- After handler.@Nonnull default Route.Collection after(String pattern, Route.After handler, Route.After... next)
{
after("*", (req, rsp, result) -> {
// your code goes here
return result;
});
}
You are allowed to modify the request, response and result objects. The handler returns a
Result
which can be the same or an entirely new Result
.
A after
handler must to be registered before the actual handler you want to
intercept.
{
after("/path", (req, rsp, result) -> {
// your code goes here
return result;
});
get("/path", req -> {
return "hello";
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
pattern
- Pattern to intercept.handler
- After handler.next
- Next handler.@Nonnull Route.Definition after(String method, String pattern, Route.After handler)
{
after("GET", "*", (req, rsp, result) -> {
// your code goes here
return result;
});
}
You are allowed to modify the request, response and result objects. The handler returns a
Result
which can be the same or an entirely new Result
.
A after
handler must to be registered before the actual handler you want to
intercept.
{
after("GET", "/path", (req, rsp, result) -> {
// your code goes here
return result;
});
get("/path", req -> {
return "hello";
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
method
- HTTP method to intercept.pattern
- Pattern to intercept.handler
- After handler.@Nonnull default Route.Collection after(String method, String pattern, Route.After handler, Route.After... next)
{
after("GET", "*", (req, rsp, result) -> {
// your code goes here
return result;
});
}
You are allowed to modify the request, response and result objects. The handler returns a
Result
which can be the same or an entirely new Result
.
A after
handler must to be registered before the actual handler you want to
intercept.
{
after("GET", "/path", (req, rsp, result) -> {
// your code goes here
return result;
});
get("/path", req -> {
return "hello";
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
method
- HTTP method to intercept.pattern
- Pattern to intercept.handler
- After handler.next
- Next handler.@Nonnull default Route.Definition complete(Route.Complete handler)
{
complete((req, rsp, cause) -> {
// your code goes here
});
}
You are NOT allowed to modify the request and response objects. The cause
is an
Optional
with a Throwable
useful to identify problems.
The goal of the after
handler is to probably cleanup request object and log
responses.
A complete
handler must to be registered before the actual handler you want to
intercept.
{
complete((req, rsp, cause) -> {
// your code goes here
});
get(req -> {
return "hello";
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
Suppose you have a transactional resource, like a database connection. The next example shows
you how to implement a simple and effective transaction-per-request
pattern:
{
// start transaction
before((req, rsp) -> {
DataSource ds = req.require(DataSource.class);
Connection connection = ds.getConnection();
Transaction trx = connection.getTransaction();
trx.begin();
req.set("connection", connection);
return true;
});
// commit/rollback transaction
complete((req, rsp, cause) -> {
// unbind connection from request
try(Connection connection = req.unset("connection").get()) {
Transaction trx = connection.getTransaction();
if (cause.ifPresent()) {
trx.rollback();
} else {
trx.commit();
}
}
});
// your transactional routes goes here
get("/api/something", req -> {
Connection connection = req.get("connection");
// work with connection
});
}
handler
- Complete handler.@Nonnull default Route.Collection complete(Route.Complete handler, Route.Complete... next)
{
complete((req, rsp, cause) -> {
// your code goes here
});
}
You are NOT allowed to modify the request and response objects. The cause
is an
Optional
with a Throwable
useful to identify problems.
The goal of the after
handler is to probably cleanup request object and log
responses.
A complete
handler must to be registered before the actual handler you want to
intercept.
{
complete((req, rsp, cause) -> {
// your code goes here
});
get(req -> {
return "hello";
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
Suppose you have a transactional resource, like a database connection. The next example shows
you how to implement a simple and effective transaction-per-request
pattern:
{
// start transaction
before((req, rsp) -> {
DataSource ds = req.require(DataSource.class);
Connection connection = ds.getConnection();
Transaction trx = connection.getTransaction();
trx.begin();
req.set("connection", connection);
return true;
});
// commit/rollback transaction
complete((req, rsp, cause) -> {
// unbind connection from request
try(Connection connection = req.unset("connection").get()) {
Transaction trx = connection.getTransaction();
if (cause.ifPresent()) {
trx.rollback();
} else {
trx.commit();
}
}
});
// your transactional routes goes here
get("/api/something", req -> {
Connection connection = req.get("connection");
// work with connection
});
}
handler
- Complete handler.next
- Next handler.@Nonnull default Route.Definition complete(String pattern, Route.Complete handler)
{
complete("*", (req, rsp, cause) -> {
// your code goes here
});
}
You are NOT allowed to modify the request and response objects. The cause
is an
Optional
with a Throwable
useful to identify problems.
The goal of the complete
handler is to probably cleanup request object and log
responses.
A complete
handler must to be registered before the actual handler you want to
intercept.
{
complete("/path", (req, rsp, cause) -> {
// your code goes here
});
get("/path", req -> {
return "hello";
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
Suppose you have a transactional resource, like a database connection. The next example shows
you how to implement a simple and effective transaction-per-request
pattern:
{
// start transaction
before("/api/*", (req, rsp) -> {
DataSource ds = req.require(DataSource.class);
Connection connection = ds.getConnection();
Transaction trx = connection.getTransaction();
trx.begin();
req.set("connection", connection);
return true;
});
// commit/rollback transaction
complete("/api/*", (req, rsp, cause) -> {
// unbind connection from request
try(Connection connection = req.unset("connection").get()) {
Transaction trx = connection.getTransaction();
if (cause.ifPresent()) {
trx.rollback();
} else {
trx.commit();
}
}
});
// your transactional routes goes here
get("/api/something", req -> {
Connection connection = req.get("connection");
// work with connection
});
}
pattern
- Pattern to intercept.handler
- Complete handler.@Nonnull default Route.Collection complete(String pattern, Route.Complete handler, Route.Complete... next)
{
complete("*", (req, rsp, cause) -> {
// your code goes here
});
}
You are NOT allowed to modify the request and response objects. The cause
is an
Optional
with a Throwable
useful to identify problems.
The goal of the complete
handler is to probably cleanup request object and log
responses.
A complete
handler must to be registered before the actual handler you want to
intercept.
{
complete("/path", (req, rsp, cause) -> {
// your code goes here
});
get("/path", req -> {
return "hello";
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
Suppose you have a transactional resource, like a database connection. The next example shows
you how to implement a simple and effective transaction-per-request
pattern:
{
// start transaction
before("/api/*", (req, rsp) -> {
DataSource ds = req.require(DataSource.class);
Connection connection = ds.getConnection();
Transaction trx = connection.getTransaction();
trx.begin();
req.set("connection", connection);
return true;
});
// commit/rollback transaction
complete("/api/*", (req, rsp, cause) -> {
// unbind connection from request
try(Connection connection = req.unset("connection").get()) {
Transaction trx = connection.getTransaction();
if (cause.ifPresent()) {
trx.rollback();
} else {
trx.commit();
}
}
});
// your transactional routes goes here
get("/api/something", req -> {
Connection connection = req.get("connection");
// work with connection
});
}
pattern
- Pattern to intercept.handler
- Complete handler.next
- Next handler.@Nonnull Route.Definition complete(String method, String pattern, Route.Complete handler)
{
complete("*", "*", (req, rsp, cause) -> {
// your code goes here
});
}
You are NOT allowed to modify the request and response objects. The cause
is an
Optional
with a Throwable
useful to identify problems.
The goal of the complete
handler is to probably cleanup request object and log
responses.
A complete
handler must to be registered before the actual handler you want to
intercept.
{
complete("*", "/path", (req, rsp, cause) -> {
});
get("/path", req -> {
return "hello";
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
Suppose you have a transactional resource, like a database connection. The next example shows
you how to implement a simple and effective transaction-per-request
pattern:
{
// start transaction
before((req, rsp) -> {
DataSource ds = req.require(DataSource.class);
Connection connection = ds.getConnection();
Transaction trx = connection.getTransaction();
trx.begin();
req.set("connection", connection);
return true;
});
// commit/rollback transaction
complete((req, rsp, cause) -> {
// unbind connection from request
try(Connection connection = req.unset("connection")) {
Transaction trx = connection.getTransaction();
if (cause.ifPresent()) {
trx.rollback();
} else {
trx.commit();
}
}
});
// your transactional routes goes here
get("/my-trx-route", req -> {
Connection connection = req.get("connection");
// work with connection
});
}
method
- HTTP method to intercept.pattern
- Pattern to intercept.handler
- Complete handler.@Nonnull default Route.Collection complete(String method, String pattern, Route.Complete handler, Route.Complete... next)
{
complete("*", "*", (req, rsp, cause) -> {
// your code goes here
});
}
You are NOT allowed to modify the request and response objects. The cause
is an
Optional
with a Throwable
useful to identify problems.
The goal of the complete
handler is to probably cleanup request object and log
responses.
A complete
handler must to be registered before the actual handler you want to
intercept.
{
complete("*", "/path", (req, rsp, cause) -> {
});
get("/path", req -> {
return "hello";
});
}
If you reverse the order then it won't work.
Remember: routes are executed in the order they are defined and the pipeline is executed as long you don't generate a response.
Suppose you have a transactional resource, like a database connection. The next example shows
you how to implement a simple and effective transaction-per-request
pattern:
{
// start transaction
before((req, rsp) -> {
DataSource ds = req.require(DataSource.class);
Connection connection = ds.getConnection();
Transaction trx = connection.getTransaction();
trx.begin();
req.set("connection", connection);
return true;
});
// commit/rollback transaction
complete((req, rsp, cause) -> {
// unbind connection from request
try(Connection connection = req.unset("connection")) {
Transaction trx = connection.getTransaction();
if (cause.ifPresent()) {
trx.rollback();
} else {
trx.commit();
}
}
});
// your transactional routes goes here
get("/my-trx-route", req -> {
Connection connection = req.get("connection");
// work with connection
});
}
method
- HTTP method to intercept.pattern
- Pattern to intercept.handler
- Complete handler.next
- Next handler.@Nonnull default WebSocket.Definition ws(String path, WebSocket.OnOpen1 handler)
ws("/ws", (socket) -> { // connected socket.onMessage(message -> { System.out.println(message); }); socket.send("Connected"): });
path
- A path pattern.handler
- A connect callback.@Nonnull WebSocket.Definition ws(String path, WebSocket.OnOpen handler)
ws("/ws", (req, socket) -> { // connected socket.onMessage(message -> { System.out.println(message); }); socket.send("Connected"): });
path
- A path pattern.handler
- A connect callback.@Nonnull default <T> WebSocket.Definition ws(Class<? extends WebSocket.OnMessage<T>> handler)
ws(MyHandler.class);
T
- Message type.handler
- A message callback.@Nonnull <T> WebSocket.Definition ws(String path, Class<? extends WebSocket.OnMessage<T>> handler)
ws("/ws", MyHandler.class);
T
- Message type.path
- A path pattern.handler
- A message callback.@Nonnull Route.Definition sse(String path, Sse.Handler handler)
{
sse("/path",(req, sse) -> {
// 1. connected
sse.send("data"); // 2. send/push data
});
}
path
- Event path.handler
- Callback. It might executed in a different thread (web server choice).@Nonnull Route.Definition sse(String path, Sse.Handler1 handler)
{
sse("/path", sse -> {
// 1. connected
sse.send("data"); // 2. send/push data
});
}
path
- Event path.handler
- Callback. It might executed in a different thread (web server choice).@Nonnull Route.Collection with(Runnable callback)
{
with(() -> {
get("/foo", ...);
get("/bar", ...);
get("/etc", ...);
...
}).attr("v1", "k1")
.excludes("/public/**");
}
All the routes wrapped by with
will have a v1
attribute and will
excludes/ignores a /public
request.callback
- Route callback.@Nonnull Router map(Route.Mapper<?> mapper)
{
mapper((Integer v) -> v * 2);
mapper(v -> Integer.parseInt(v.toString()));
get("/four", () -> "2");
}
A call to /four
outputs 4
. Mapper are applied in reverse order.mapper
- Route mapper to append.@Nonnull Router err(Err.Handler err)
Err.DefHandler
does content
negotation and this method allow to override/complement default handler.
This is a catch all error handler.
text/html
header. Then, the default err handler will
ask to a View.Engine
to render the err
view.
The default model has these attributes:
message: exception string stacktrace: exception stack-trace as an array of string status: status code, like 400 reason: status code reason, like BAD REQUESTHere is a simply
public/err.html
error page:
<html> <body> {{ "{{status" }}}}:{{ "{{reason" }}}} </body> </html>HTTP status code will be set too.
err
- A route error handler.@Nonnull default Router err(Class<? extends Throwable> type, Err.Handler handler)
type
- Exception type. The error handler will be executed if the current exception is an
instance of this type.handler
- A route error handler.@Nonnull default Router err(int statusCode, Err.Handler handler)
statusCode
- The status code to match.handler
- A route error handler.@Nonnull default Router err(Status code, Err.Handler handler)
code
- The status code to match.handler
- A route error handler.@Nonnull default Router err(Predicate<Status> predicate, Err.Handler handler)
predicate
- Apply the error handler if the predicate evaluates to true
.handler
- A route error handler.@Nonnull Route.OneArgHandler promise(Deferred.Initializer initializer)
Deferred
results run in the current thread.
This is intentional because application code (your code) always run in a worker thread. There
is a thread pool of 100
worker threads, defined by the property:
server.threads.Max
.
That's why a Deferred
result runs in the current thread (no need to use a new thread),
unless you want to apply a different thread model and or use a reactive/async library.
As a final thought you might want to reduce the number of worker thread if you are going to a
build a full reactive/async application.
{ get("/async", promise(deferred -> { try { deferred.resolve(...); // success value } catch (Exception ex) { deferred.reject(ex); // error value } })); }
This method is useful for integrating reactive/async libraries. Here is an example on how to use RxJava:
{
get("/rx", promise(deferred -> {
Observable.create(s -> {
s.onNext(...);
s.onCompleted();
}).subscribeOn(Schedulers.computation())
.subscribe(deferred::resolve, deferred::reject);
}));
}
This is just an example because there is a Rx module.
Checkout the deferred(org.jooby.Route.ZeroArgHandler)
methods to see how to use a
plain Executor
.
initializer
- Deferred initializer.Deferred
@Nonnull Route.OneArgHandler promise(String executor, Deferred.Initializer initializer)
promise(org.jooby.Deferred.Initializer)
but allow you to specify an Executor
to use. See Jooby.executor(Executor)
and Jooby.executor(String, Executor)
.
{ executor("forkjoin", new ForkJoinPool()); get("/async", promise("forkjoin", deferred -> { try { deferred.resolve(...); // success value } catch (Exception ex) { deferred.reject(ex); // error value } })); }
Checkout the deferred(org.jooby.Route.ZeroArgHandler)
methods to see how to use a
plain Executor
.
executor
- Executor to run the deferred.initializer
- Deferred initializer.Deferred
@Nonnull Route.OneArgHandler promise(Deferred.Initializer0 initializer)
promise(org.jooby.Deferred.Initializer)
but give you access to Request
.
{ ExecutorService executor = ...; get("/async", promise((req, deferred) -> { executor.execute(() -> { try { deferred.resolve(req.param("param").value()); // success value } catch (Exception ex) { deferred.reject(ex); // error value } }); })); }
initializer
- Deferred initializer.Deferred
@Nonnull Route.OneArgHandler promise(String executor, Deferred.Initializer0 initializer)
promise(String, org.jooby.Deferred.Initializer)
but give you access to
Request
.
{ get("/async", promise("myexec", (req, deferred) -> { // resolve a success value deferred.resolve(req.param("param").value()); })); }
executor
- Executor to run the deferred.initializer
- Deferred initializer.Deferred
@Nonnull default Route.ZeroArgHandler deferred(String executor, Route.OneArgHandler handler)
promise(org.jooby.Deferred.Initializer)
. To use ideally with one
or more Executor
:
{
executor("cached", Executors.newCachedExecutor());
get("/fork", deferred("cached", req -> {
return req.param("value").value();
}));
}
This handler automatically Deferred.resolve(Object)
or
Deferred.reject(Throwable)
a route handler response.executor
- Executor to run the deferred.handler
- Application block.@Nonnull default Route.ZeroArgHandler deferred(Route.OneArgHandler handler)
promise(org.jooby.Deferred.Initializer)
.
Using the default executor (current thread):
{
get("/fork", deferred(req -> {
return req.param("value").value();
}));
}
Using a custom executor:
{
executor(new ForkJoinPool());
get("/fork", deferred(req -> {
return req.param("value").value();
}));
}
This handler automatically Deferred.resolve(Object)
or
Deferred.reject(Throwable)
a route handler response.handler
- Application block.@Nonnull default Route.ZeroArgHandler deferred(String executor, Route.ZeroArgHandler handler)
promise(org.jooby.Deferred.Initializer)
. To use ideally with one
or more Executor
:
{
executor("cached", Executors.newCachedExecutor());
get("/fork", deferred("cached", () -> {
return "OK";
}));
}
This handler automatically Deferred.resolve(Object)
or
Deferred.reject(Throwable)
a route handler response.executor
- Executor to run the deferred.handler
- Application block.@Nonnull default Route.ZeroArgHandler deferred(Route.ZeroArgHandler handler)
promise(org.jooby.Deferred.Initializer)
.
Using the default executor (current thread):
{
get("/fork", deferred(() -> {
return req.param("value").value();
}));
}
Using a custom executor:
{
executor(new ForkJoinPool());
get("/fork", deferred(() -> {
return req.param("value").value();
}));
}
This handler automatically Deferred.resolve(Object)
or
Deferred.reject(Throwable)
a route handler response.handler
- Application block.Copyright © 2019. All rights reserved.