public class Router<T>
extends java.lang.Object
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:
router.removeTarget(IndexHandler.class);
router.removePath("/articles");
route(HttpMethod, String)
.
From the RouteResult
you can extract params embedded in
the path and from the query part of the request URI.
notFound(Object)
. It will be used as the target
when there's no match.
router.notFound(My404Handler.class);
path(HttpMethod, Object, Object...)
or path(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"
allowedMethods(String)
.
For OPTIONS *
, use allAllowedMethods()
.Constructor and Description |
---|
Router() |
Modifier and Type | Method and Description |
---|---|
Router |
addRoute(io.netty.handler.codec.http.HttpMethod method,
java.lang.String path,
T target)
Add route to the "other" section.
|
Router |
addRouteFirst(io.netty.handler.codec.http.HttpMethod method,
java.lang.String path,
T target)
Add route to the "first" section.
|
Router |
addRouteLast(io.netty.handler.codec.http.HttpMethod method,
java.lang.String path,
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 |
ANY_FIRST(java.lang.String path,
T target) |
Router |
ANY_LAST(java.lang.String path,
T target) |
Router |
ANY(java.lang.String path,
T target) |
Router |
CONNECT_FIRST(java.lang.String path,
T target) |
Router |
CONNECT_LAST(java.lang.String path,
T target) |
Router |
CONNECT(java.lang.String path,
T target) |
Router |
DELETE_FIRST(java.lang.String path,
T target) |
Router |
DELETE_LAST(java.lang.String path,
T target) |
Router |
DELETE(java.lang.String path,
T target) |
Router |
GET_FIRST(java.lang.String path,
T target) |
Router |
GET_LAST(java.lang.String path,
T target) |
Router |
GET(java.lang.String path,
T target) |
Router |
HEAD_FIRST(java.lang.String path,
T target) |
Router |
HEAD_LAST(java.lang.String path,
T target) |
Router |
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 |
notFound(T target)
Sets the fallback target for use when there's no match at
route(HttpMethod, String) . |
Router |
OPTIONS_FIRST(java.lang.String path,
T target) |
Router |
OPTIONS_LAST(java.lang.String path,
T target) |
Router |
OPTIONS(java.lang.String path,
T target) |
Router |
PATCH_FIRST(java.lang.String path,
T target) |
Router |
PATCH_LAST(java.lang.String path,
T target) |
Router |
PATCH(java.lang.String path,
T target) |
java.lang.String |
path(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 path.
|
java.lang.String |
path(T target,
java.lang.Object... params)
Given a target and params, this method tries to do the reverse routing
and returns the path.
|
Router |
POST_FIRST(java.lang.String path,
T target) |
Router |
POST_LAST(java.lang.String path,
T target) |
Router |
POST(java.lang.String path,
T target) |
Router |
PUT_FIRST(java.lang.String path,
T target) |
Router |
PUT_LAST(java.lang.String path,
T target) |
Router |
PUT(java.lang.String path,
T target) |
void |
removePath(java.lang.String path)
Removes the route specified by the path.
|
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 |
TRACE_FIRST(java.lang.String path,
T target) |
Router |
TRACE_LAST(java.lang.String path,
T target) |
Router |
TRACE(java.lang.String path,
T target) |
public T notFound()
route(HttpMethod, String)
.public int size()
public Router addRouteFirst(io.netty.handler.codec.http.HttpMethod method, java.lang.String path, T target)
public Router addRoute(io.netty.handler.codec.http.HttpMethod method, java.lang.String path, T target)
public Router addRouteLast(io.netty.handler.codec.http.HttpMethod method, java.lang.String path, T target)
public Router notFound(T target)
route(HttpMethod, String)
.public void removePath(java.lang.String path)
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)
OPTIONS *
, use allAllowedMethods()
instead of this method.public java.util.Set<io.netty.handler.codec.http.HttpMethod> allAllowedMethods()
OPTIONS *
.public java.lang.String path(io.netty.handler.codec.http.HttpMethod method, T target, java.lang.Object... params)
placeholder name -> value
or ordered values. If a param doesn't have a placeholder, it will be put
to the query part of the path.null
if there's no matchpublic java.lang.String path(T target, java.lang.Object... params)
placeholder name -> value
or ordered values. If a param doesn't have a placeholder, it will be put
to the query part of the path.null
if there's no matchpublic java.lang.String toString()
toString
in class java.lang.Object