public class Router<T>
extends java.lang.Object
Routes are devided into 3 sections: "first", "last", and "other". Routes in "first" are matched first, then in "other", then in "last".
Route targets can be any type. In the below example, targets are classes:
Router<Class> router = new Router<Class>()
.GET ("/articles", IndexHandler.class)
.GET ("/articles/:id", ShowHandler.class)
.POST ("/articles", CreateHandler.class)
.GET ("/download/:*", DownloadHandler.class) // ":*" must be the last token
.GET_FIRST("/articles/new", NewHandler.class); // This will be matched first
Slashes at both ends are ignored. These are the same:
router.GET("articles", IndexHandler.class);
router.GET("/articles", IndexHandler.class);
router.GET("/articles/", IndexHandler.class);
You can remove routes by target or by path pattern:
router.removeTarget(IndexHandler.class);
router.removePathPattern("/articles");
Use route(HttpMethod, String).
From the RouteResult you can extract params embedded in
the path and from the query part of the request URI.
Use notFound(Object). It will be used as the target
when there's no match.
router.notFound(My404Handler.class);
Use uri(HttpMethod, Object, Object...) or uri(Object, Object...):
router.path(HttpMethod.GET, IndexHandler.class);
// Returns "/articles"
You can skip HTTP method if there's no confusion:
router.path(CreateHandler.class);
// Also returns "/articles"
You can specify params as map:
// Things in params will be converted to String
Map<Object, Object> params = new HashMap<Object, Object>();
params.put("id", 123);
router.path(ShowHandler.class, params);
// Returns "/articles/123"
Convenient way to specify params:
router.path(ShowHandler.class, "id", 123);
// Returns "/articles/123"
If you want to implement
OPTIONS
or
CORS,
you can use allowedMethods(String).
For OPTIONS *, use allAllowedMethods().
| Constructor and Description |
|---|
Router() |
| Modifier and Type | Method and Description |
|---|---|
Router<T> |
addRoute(io.netty.handler.codec.http.HttpMethod method,
java.lang.String pathPattern,
T target)
Add route to the "other" section.
|
Router<T> |
addRouteFirst(io.netty.handler.codec.http.HttpMethod method,
java.lang.String pathPattern,
T target)
Add route to the "first" section.
|
Router<T> |
addRouteLast(io.netty.handler.codec.http.HttpMethod method,
java.lang.String pathPattern,
T target)
Add route to the "last" section.
|
java.util.Set<io.netty.handler.codec.http.HttpMethod> |
allAllowedMethods()
Returns all methods that this router handles.
|
java.util.Set<io.netty.handler.codec.http.HttpMethod> |
allowedMethods(java.lang.String uri)
Returns allowed methods for a specific URI.
|
Router<T> |
ANY_FIRST(java.lang.String path,
T target) |
Router<T> |
ANY_LAST(java.lang.String path,
T target) |
Router<T> |
ANY(java.lang.String path,
T target) |
Router<T> |
CONNECT_FIRST(java.lang.String path,
T target) |
Router<T> |
CONNECT_LAST(java.lang.String path,
T target) |
Router<T> |
CONNECT(java.lang.String path,
T target) |
Router<T> |
DELETE_FIRST(java.lang.String path,
T target) |
Router<T> |
DELETE_LAST(java.lang.String path,
T target) |
Router<T> |
DELETE(java.lang.String path,
T target) |
Router<T> |
GET_FIRST(java.lang.String path,
T target) |
Router<T> |
GET_LAST(java.lang.String path,
T target) |
Router<T> |
GET(java.lang.String path,
T target) |
Router<T> |
HEAD_FIRST(java.lang.String path,
T target) |
Router<T> |
HEAD_LAST(java.lang.String path,
T target) |
Router<T> |
HEAD(java.lang.String path,
T target) |
T |
notFound()
Returns the fallback target for use when there's no match at
route(HttpMethod, String). |
Router<T> |
notFound(T target)
Sets the fallback target for use when there's no match at
route(HttpMethod, String). |
Router<T> |
OPTIONS_FIRST(java.lang.String path,
T target) |
Router<T> |
OPTIONS_LAST(java.lang.String path,
T target) |
Router<T> |
OPTIONS(java.lang.String path,
T target) |
Router<T> |
PATCH_FIRST(java.lang.String path,
T target) |
Router<T> |
PATCH_LAST(java.lang.String path,
T target) |
Router<T> |
PATCH(java.lang.String path,
T target) |
Router<T> |
POST_FIRST(java.lang.String path,
T target) |
Router<T> |
POST_LAST(java.lang.String path,
T target) |
Router<T> |
POST(java.lang.String path,
T target) |
Router<T> |
PUT_FIRST(java.lang.String path,
T target) |
Router<T> |
PUT_LAST(java.lang.String path,
T target) |
Router<T> |
PUT(java.lang.String path,
T target) |
void |
removePathPattern(java.lang.String pathPattern)
Removes the route specified by the path pattern.
|
void |
removeTarget(T target)
Removes all routes leading to the target.
|
RouteResult<T> |
route(io.netty.handler.codec.http.HttpMethod method,
java.lang.String uri)
If there's no match, returns the result with
notFound
as the target if it is set, otherwise returns null. |
int |
size()
Returns the number of routes in this router.
|
java.lang.String |
toString()
Returns visualized routing rules.
|
Router<T> |
TRACE_FIRST(java.lang.String path,
T target) |
Router<T> |
TRACE_LAST(java.lang.String path,
T target) |
Router<T> |
TRACE(java.lang.String path,
T target) |
java.lang.String |
uri(io.netty.handler.codec.http.HttpMethod method,
T target,
java.lang.Object... params)
Given a target and params, this method tries to do the reverse routing
and returns the URI.
|
java.lang.String |
uri(T target,
java.lang.Object... params)
Given a target and params, this method tries to do the reverse routing
and returns the URI.
|
public T notFound()
route(HttpMethod, String).public int size()
public Router<T> addRouteFirst(io.netty.handler.codec.http.HttpMethod method, java.lang.String pathPattern, T target)
A path pattern can only point to one target. This method does nothing if the pattern has already been added.
public Router<T> addRoute(io.netty.handler.codec.http.HttpMethod method, java.lang.String pathPattern, T target)
A path pattern can only point to one target. This method does nothing if the pattern has already been added.
public Router<T> addRouteLast(io.netty.handler.codec.http.HttpMethod method, java.lang.String pathPattern, T target)
A path pattern can only point to one target. This method does nothing if the pattern has already been added.
public Router<T> notFound(T target)
route(HttpMethod, String).public void removePathPattern(java.lang.String pathPattern)
public void removeTarget(T target)
public RouteResult<T> route(io.netty.handler.codec.http.HttpMethod method, java.lang.String uri)
notFound
as the target if it is set, otherwise returns null.public java.util.Set<io.netty.handler.codec.http.HttpMethod> allowedMethods(java.lang.String uri)
For OPTIONS *, use allAllowedMethods() instead of this method.
public java.util.Set<io.netty.handler.codec.http.HttpMethod> allAllowedMethods()
OPTIONS *.public java.lang.String uri(io.netty.handler.codec.http.HttpMethod method,
T target,
java.lang.Object... params)
Placeholders in the path pattern will be filled with the params.
The params can be a map of placeholder name -> value
or ordered values.
If a param doesn't have a corresponding placeholder, it will be put to the query part of the result URI.
null if there's no matchpublic java.lang.String uri(T target, java.lang.Object... params)
Placeholders in the path pattern will be filled with the params.
The params can be a map of placeholder name -> value
or ordered values.
If a param doesn't have a corresponding placeholder, it will be put to the query part of the result URI.
null if there's no matchpublic java.lang.String toString()
toString in class java.lang.Object