public class Router<T> extends Object
RouterHandler
which was dropped in tv.cntt:netty-router
2.X.X. Original code:
https://github.com/sinetja/netty-router/blob/2.2.0/src/main/java/io/netty/handler/codec/http/router/Router.java
Router that contains information about both route matching orders and HTTP request methods.
Routes are guaranteed to be matched in order of their addition.
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.removePathPattern("/articles");
To match requests use 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)
will be used as the target when there's no match.
router.notFound(My404Handler.class);
Constructor and Description |
---|
Router() |
Modifier and Type | Method and Description |
---|---|
Router<T> |
addAny(String path,
T target) |
Router<T> |
addConnect(String path,
T target) |
Router<T> |
addDelete(String path,
T target) |
Router<T> |
addGet(String path,
T target) |
Router<T> |
addHead(String path,
T target) |
Router<T> |
addOptions(String path,
T target) |
Router<T> |
addPatch(String path,
T target) |
Router<T> |
addPost(String path,
T target) |
Router<T> |
addPut(String path,
T target) |
Router<T> |
addRoute(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod method,
String pathPattern,
T target)
Add route.
|
Router<T> |
addTrace(String path,
T target) |
Set<org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod> |
allAllowedMethods()
Returns all methods that this router handles.
|
Set<org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod> |
allowedMethods(String uri)
Returns allowed methods for a specific URI.
|
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) . |
void |
removePathPattern(String pathPattern)
Removes the route specified by the path pattern.
|
RouteResult<T> |
route(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod method,
String path)
If there's no match, returns the result with
notFound as the target
if it is set, otherwise returns null . |
RouteResult<T> |
route(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod method,
String path,
Map<String,List<String>> queryParameters) |
int |
size()
Returns the number of routes in this router.
|
String |
toString()
Returns visualized routing rules.
|
public T notFound()
route(HttpMethod,
String)
.public int size()
public Router<T> addRoute(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod method, 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(String pathPattern)
public RouteResult<T> route(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod method, String path)
notFound
as the target
if it is set, otherwise returns null
.public RouteResult<T> route(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod method, String path, Map<String,List<String>> queryParameters)
public Set<org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod> allowedMethods(String uri)
For OPTIONS *
, use allAllowedMethods()
instead of this method.
public Set<org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpMethod> allAllowedMethods()
OPTIONS *
.public String toString()
Copyright © 2014–2024 The Apache Software Foundation. All rights reserved.