public interface Route
There are two types of handlers: Route.Handler
and Route.Filter
. They behave very
similar, except that a Route.Filter
can decide if the next route handler can be executed
or not. For example:
get("/filter", (req, rsp, chain) -> { if (someCondition) { chain.next(req, rsp); } else { // respond, throw err, etc... } });While a
Route.Handler
always execute the next handler:
get("/path", (req, rsp) -> { rsp.send("handler"); }); // filter version get("/path", (req, rsp, chain) -> { rsp.send("handler"); chain.next(req, rsp); });
Jooby supports Ant-style path patterns:
Some examples:
com/t?st.html
- matches com/test.html
but also com/tast.jsp
or
com/txst.html
com/*.html
- matches all .html
files in the com
directorycom/**/test.html
- matches all test.html
files underneath the
com
path**
/*
- matches any path at any level.*
- matches any path at any level, shorthand for **
/*
.Jooby supports path parameters too:
Some examples:
/user/{id}
- /user/* and give you access to the id
var. /user/:id
- /user/* and give you access to the id
var. /user/{id:\\d+}
- /user/[digits] and give you access to the numeric
id
var.Routes are executed in the order they are defined, for example:
get("/", (req, rsp) -> { log.info("first"); // start here and go to second }); get("/", (req, rsp) -> { log.info("second"); // execute after first and go to final }); get("/", (req, rsp) -> { rsp.send("final"); // done! });Please note first and second routes are converted to a filter, so previous example is the same as:
get("/", (req, rsp, chain) -> { log.info("first"); // start here and go to second chain.next(req, rsp); }); get("/", (req, rsp, chain) -> { log.info("second"); // execute after first and go to final chain.next(req, rsp); }); get("/", (req, rsp) -> { rsp.send("final"); // done! });
An inline route can be defined using Lambda expressions, like:
get("/", (request, response) -> { response.send("Hello Jooby"); });Due to the use of lambdas a route is a singleton and you should NOT use global variables. For example this is a bad practice:
List <String > names = new ArrayList <>(); // names produces side effects get("/", (req, rsp) -> { names.add(req.param("name").value(); // response will be different between calls. rsp.send(names); });
An external route can be defined by using a route class
, like:
get("/", handler(ExternalRoute.class)); //or ... // ExternalRoute.java public class ExternalRoute implements Route.Handler { public void handle(Request req, Response rsp) throws Exception { rsp.send("Hello Jooby"); } }
A Mvc Route use annotations to define routes:
route(MyRoute.class); ... // MyRoute.java @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
.
Modifier and Type | Interface and Description |
---|---|
static interface |
Route.Chain
Chain of routes to be executed.
|
static class |
Route.Definition
DSL for customize routes.
|
static class |
Route.Definitions
Collection of
Route.Definition useful for registering/setting route options at once. |
static interface |
Route.Filter
Filter is like a
Route.Handler but it decided if the next route handler in the chain
can be executed or not. |
static class |
Route.Forwarding
A forwarding route.
|
static interface |
Route.Handler
A route handler/callback.
|
static interface |
Route.MethodHandler
A handler from a MVC route, it extends
Route.Handler by adding a reference to the method
behind this route. |
static class |
Route.Namespace |
static interface |
Route.OneArgHandler
A route handler/callback that doesn't require a
Response object. |
static interface |
Route.ZeroArgHandler
|
Modifier and Type | Field and Description |
---|---|
static List<String> |
METHODS
Well known HTTP methods.
|
Modifier and Type | Method and Description |
---|---|
List<MediaType> |
consumes() |
String |
method() |
String |
name() |
String |
path() |
String |
pattern() |
List<MediaType> |
produces() |
Map<Object,String> |
vars()
Path variables, either named or by index (capturing group).
|
String path()
String method()
String pattern()
String name()
"anonymous"
Map<Object,String> vars()
/path/:varVariable
var
is accesible by name: var
or index: 0
.List<MediaType> consumes()
* / *
.Copyright © 2015. All rights reserved.