Package play.routing
Class RoutingDsl
- Object
-
- play.routing.RoutingDsl
-
public class RoutingDsl extends Object
A DSL for building a router.This DSL matches requests based on method and a path pattern, and is able to extract up to three parameters out of the path pattern to pass into lambdas.
The passed in lambdas may optionally declare the types of the input parameters. If they don't, the JVM will infer a type of Object, but the parameters themselves are passed in as Strings. Supported types are java.lang.Integer, java.lang.Long, java.lang.Float, java.lang.Double, java.lang.Boolean, and any class that extends play.mvc.PathBindable. The router will attempt to decode parameters using a PathBindable for each of those types, if it fails it will return a 400 error.
Example usage:
import javax.inject.*; import play.mvc.*; import play.routing.*; import play.libs.json.*; import play.api.routing.Router; public class MyRouterBuilder extends Controller { private final RoutingDsl routingDsl; \@Inject public MyRouterBuilder(RoutingDsl routingDsl) { this.routingDsl = routingDsl; } public Router getRouter() { return this.routingDsl .GET("/hello/:to").routingTo((req, to) -> ok("Hello " + to)) .POST("/api/items/:id").routingAsync((Http.Request req, Integer id) -> { return Items.save(id, Json.fromJson(req.body().asJson(), Item.class) ).map(result -> ok("Saved item with id " + id)); }) .build(); } }
The path pattern supports three different types of parameters, path segment parameters, prefixed with :, full path parameters, prefixed with *, and regular expression parameters, prefixed with $ and post fixed with a regular expression in angled braces.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
RoutingDsl.PathPatternMatcher
A matcher for routes.
-
Constructor Summary
Constructors Constructor Description RoutingDsl(BodyParser.Default bodyParser)
RoutingDsl(BodyParser.Default bodyParser, play.core.j.JavaContextComponents contextComponents)
Deprecated.Deprecated as of 2.8.0.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Router
build()
Build the router.RoutingDsl.PathPatternMatcher
DELETE(String pathPattern)
Create a DELETE route for the given path pattern.static RoutingDsl
fromComponents(BuiltInComponents components)
RoutingDsl.PathPatternMatcher
GET(String pathPattern)
Create a GET route for the given path pattern.RoutingDsl.PathPatternMatcher
HEAD(String pathPattern)
Create a HEAD route for the given path pattern.RoutingDsl.PathPatternMatcher
match(String method, String pathPattern)
Create a route for the given method and path pattern.RoutingDsl.PathPatternMatcher
OPTIONS(String pathPattern)
Create a OPTIONS route for the given path pattern.RoutingDsl.PathPatternMatcher
PATCH(String pathPattern)
Create a PATCH route for the given path pattern.RoutingDsl.PathPatternMatcher
POST(String pathPattern)
Create a POST route for the given path pattern.RoutingDsl.PathPatternMatcher
PUT(String pathPattern)
Create a PUT route for the given path pattern.
-
-
-
Constructor Detail
-
RoutingDsl
@Inject public RoutingDsl(BodyParser.Default bodyParser)
-
RoutingDsl
@Deprecated public RoutingDsl(BodyParser.Default bodyParser, play.core.j.JavaContextComponents contextComponents)
Deprecated.Deprecated as of 2.8.0. Use constructor without JavaContextComponents
-
-
Method Detail
-
fromComponents
public static RoutingDsl fromComponents(BuiltInComponents components)
-
GET
public RoutingDsl.PathPatternMatcher GET(String pathPattern)
Create a GET route for the given path pattern.- Parameters:
pathPattern
- The path pattern.- Returns:
- A GET route matcher.
-
HEAD
public RoutingDsl.PathPatternMatcher HEAD(String pathPattern)
Create a HEAD route for the given path pattern.- Parameters:
pathPattern
- The path pattern.- Returns:
- A HEAD route matcher.
-
POST
public RoutingDsl.PathPatternMatcher POST(String pathPattern)
Create a POST route for the given path pattern.- Parameters:
pathPattern
- The path pattern.- Returns:
- A POST route matcher.
-
PUT
public RoutingDsl.PathPatternMatcher PUT(String pathPattern)
Create a PUT route for the given path pattern.- Parameters:
pathPattern
- The path pattern.- Returns:
- A PUT route matcher.
-
DELETE
public RoutingDsl.PathPatternMatcher DELETE(String pathPattern)
Create a DELETE route for the given path pattern.- Parameters:
pathPattern
- The path pattern.- Returns:
- A DELETE route matcher.
-
PATCH
public RoutingDsl.PathPatternMatcher PATCH(String pathPattern)
Create a PATCH route for the given path pattern.- Parameters:
pathPattern
- The path pattern.- Returns:
- A PATCH route matcher.
-
OPTIONS
public RoutingDsl.PathPatternMatcher OPTIONS(String pathPattern)
Create a OPTIONS route for the given path pattern.- Parameters:
pathPattern
- The path pattern.- Returns:
- A OPTIONS route matcher.
-
match
public RoutingDsl.PathPatternMatcher match(String method, String pathPattern)
Create a route for the given method and path pattern.- Parameters:
method
- The method;pathPattern
- The path pattern.- Returns:
- A route matcher.
-
build
public Router build()
Build the router.- Returns:
- The built router.
-
-