Routes are a key concept in Jooby. Routes are executed in the same order they are defined.
handlers
There are few type of handlers:
Route.Handler,
Route.OneArgHandlerRoute.ZeroArgHandler 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);
});
The
Route.OneArgHandler and
Route.ZeroArgHandler offers a functional version of
generating a response:
get("/path", req -> "handler");}
There is no need to call
Response#send(Object).
path patterns
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 directory
com/
**/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
**/
*.
variables
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 semantic
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!
});
script route
A script 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
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);
});
mvc Route
A Mvc Route use annotations to define routes:
{
use(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
org.jooby.mvc.Path,
org.jooby.mvc.Produces
org.jooby.mvc.Consumes.