public interface Route
There are few type of handlers: Route.Handler
, Route.OneArgHandler
Route.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");
get("/path", () -> "handler");
}
There is no need to call Response.send(Object)
.
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! });
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 <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 class |
Route.AssetDefinition
Allow to customize an asset handler.
|
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
The most advanced route handler which let you 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 that always call Route.Chain.next(Request, Response) .
|
static interface |
Route.Mapper<T>
Converts a route output to something else, see Router#map(Mapper) .
|
static interface |
Route.MethodHandler
A handler for a MVC route, it extends Route.Handler by adding a reference to the method and class behind this route.
|
static interface |
Route.OneArgHandler
A functional route handler that use the return value as HTTP response.
|
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
A functional handler that use the return value as HTTP response.
|
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 char |
OUT_OF_PATH |
static String |
PATCH |
static String |
POST |
static String |
PUT |
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() |
static String |
errpath(String path)
Mark a path as invalid.
|
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 |
renderer()
Explicit renderer to use or null .
|
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()
Source information (where the route was defined).
|
static String |
unerrpath(String path)
Remove invalid path mark when present.
|
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 char OUT_OF_PATH
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
@Nonnull String name()
Route.Chain.next(String, Request, Response)
"anonymous"
@Nonnull Map<Object,String> vars()
/path/:varVariable
var
is accessible by name: var
or index: 0
.
@Nonnull List<MediaType> consumes()
* / *
.
@Nonnull List<MediaType> produces()
* / *
.
default boolean apply(String prefix)
Route.Chain.next(String, Request, Response)
prefix
- Prefix to check for.
@Nonnull Map<String,Object> attributes()
@Nonnull default <T> T attr(String name)
T
- Attribute's type.
name
- Attribute's name.
@Nonnull String renderer()
null
.
null
.
boolean glob()
pattern()
contains a glob charecter, like ?
, *
or **
.
@Nonnull String reverse(Map<String,Object> vars)
vars
- Path variables.
@Nonnull String reverse(Object... values)
values
- Path variable values.
@Nonnull static String normalize(String path)
path
- A path to normalize.
@Nonnull static String unerrpath(String path)
path
- Path.
@Nonnull static String errpath(String path)
path
- Path.
@Nonnull Route.Source source()
@Nonnull default String print(int indent)
indent
- Indent level
Copyright © 2019. All rights reserved.