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); });
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 Path
,
Produces
Consumes
.
Modifier and Type | Interface and Description |
---|---|
static interface |
Route.After
after
|
static interface |
Route.Before
before
|
static interface |
Route.Chain
Chain of routes to be executed.
|
static class |
Route.Collection
Collection of
Route.Props useful for registering/setting route options at once. |
static interface |
Route.Complete
complete
|
static class |
Route.Definition
DSL for customize routes.
|
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 class |
Route.Group |
static interface |
Route.Handler
A route handler/callback.
|
static interface |
Route.Mapper<T>
The map operator converts a route output to something else
|
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 interface |
Route.OneArgHandler
A route handler/callback that doesn't require a
Response object. |
static interface |
Route.Props<T extends Route.Props<T>>
Common route properties, like static and global metadata via attributes, path exclusion,
produces and consumes types.
|
static interface |
Route.Source
Provides useful information about where the route was defined.
|
static interface |
Route.ZeroArgHandler
|
Modifier and Type | Field and Description |
---|---|
static String |
CONNECT |
static String |
DELETE |
static String |
GET |
static String |
HEAD |
static com.google.inject.Key<Set<Route.Definition>> |
KEY
Route key.
|
static List<String> |
METHODS
Well known HTTP methods.
|
static String |
OPTIONS |
static String |
PATCH |
static String |
POST |
static String |
PUT |
static String |
RENDERER
Renderer attribute.
|
static String |
TRACE |
Modifier and Type | Method and Description |
---|---|
default boolean |
apply(String prefix)
True, when route's name starts with the given prefix.
|
default <T> T |
attr(String name)
Attribute by name.
|
Map<String,Object> |
attributes() |
List<MediaType> |
consumes() |
boolean |
glob()
|
String |
method() |
String |
name()
Route's name, helpful for debugging but also to implement dynamic and advanced routing.
|
static String |
normalize(String path)
Normalize a path by removing double or trailing slashes.
|
String |
path() |
String |
pattern() |
default String |
print()
Print route information like: method, path, source, etc...
|
default String |
print(int indent)
Print route information like: method, path, source, etc...
|
List<MediaType> |
produces() |
String |
reverse(Map<String,Object> vars)
Recreate a route path and apply the given variables.
|
String |
reverse(Object... values)
Recreate a route path and apply the given variables.
|
Route.Source |
source() |
Map<Object,String> |
vars()
Path variables, either named or by index (capturing group).
|
static final com.google.inject.Key<Set<Route.Definition>> KEY
static final String GET
static final String POST
static final String PUT
static final String DELETE
static final String PATCH
static final String HEAD
static final String CONNECT
static final String OPTIONS
static final String TRACE
static final String RENDERER
String path()
String method()
String pattern()
String name()
Route.Chain.next(String, Request, Response)
"anonymous"
Map<Object,String> vars()
/path/:varVariable
var
is accessible by name: var
or index: 0
.List<MediaType> consumes()
* / *
.List<MediaType> produces()
* / *
.default boolean apply(String prefix)
Route.Chain.next(String, Request, Response)
prefix
- Prefix to check for.Map<String,Object> attributes()
default <T> T attr(String name)
T
- Attribute's type.name
- Attribute's name.boolean glob()
pattern()
contains a glob charecter, like ?
,
*
or **
.String reverse(Map<String,Object> vars)
vars
- Path variables.String reverse(Object... values)
values
- Path variable values.static String normalize(String path)
path
- A path to normalize.Route.Source source()
default String print(int indent)
indent
- Indent leveldefault String print()
Copyright © 2016. All rights reserved.