Routing
In this first example we are going to build a simple Veronica application with three routes:
- A homepage route at
/home
- An about route at
/about
- A fallback "not found" route
Here's a list of the required preliminary knowledge for this tutorial:
- Basic application initialization
- How to create a route
- How to initialize the application router
If you lack the preliminary knowledge, we advise you to go back to previous tutorials to get started. Here's a list of the topics that we are going to cover in this tutorial:
- Basic routing
- Working with RequestMatcher objects
Let's get started:
- First we need to define our request handlers:
RequestHandler<Request, Response> homeRH = request -> ok(Response.builder()
.body("This is the homepage")
.build());
RequestHandler<Request, Response> aboutRH = request -> ok(Response.builder()
.body("This is the about page")
.build());
RequestHandler<Request, Response> fallbackRH = request -> ok(Response.builder()
.httpStatus(HttpStatus.NOT_FOUND)
.body("We could not find what you're looking for")
.build());
Notice how the fallback request handler specifies the HTTP status of the response to be 404 NOT FOUND
. We don't need to specify the status for the other request handlers, since by default it is set to 200 OK
.
- We now need to specify the routing conditions for our routes, so that our router knows what route should handle an incoming request. To do so, we need to initialize two different RequestMatcher object. Notice that we don't need to specify a third RequestMatcher for the "not found" fallback route, since our router will automatically default to it when it does not find any other route that can handle an incoming request
RequestMatcher<Request> homeRM = request -> request.getPath().equals("/home");
RequestMatcher<Request> aboutRM = request -> request.getPath().equals("/about");
We can also use the utility class CommonRequestMatchers, which makes initializing RequestMatcher objects easier:
import static rocks.gioac96.veronica.routing.matching.CommonRequestMatchers.path;
...
RequestMatcher<Request> homeRM = path("/home");
RequestMatcher<Request> aboutRM = path("/about");
We might also want our two routes to match only against HTTP GET
requests:
RequestMatcher<Request> homeRM = get("/home");
RequestMatcher<Request> aboutRM = get("/about");
- We are now ready to initialize our router and build our application:
Route<Request, Response> homeRoute = Route.builder()
.requestMatcher(homeRM)
.requestHandler(homeRH)
.build();
Route<Request, Response> aboutRoute = Route.builder()
.requestMatcher(aboutRM)
.requestHandler(aboutRH)
.build();
Route<Request, Response> fallbackRoute = Route.builder()
.requestHandler(fallbackRH)
.build();
Router<Request, Response> router = Router.builder()
.route(homeRoute)
.route(aboutRoute)
.fallbackRoute(fallbackRoute)
.build();
int port = 8000;
Application<Request, Response> app = Application.basic(port, router);
app.start();
If we want, we can also compress our code a bit, while still maintaining a fair degree of readability:
Router<Request, Response> router = Router.builder()
.route(Route.builder()
.requestMatcher(path("/home"))
.requestHandler(req -> ok(Response.builder()
.body("This is the homepage")
.build())
)
.build()
)
.route(Route.builder()
.requestMatcher(path("/about"))
.requestHandler(req -> ok(Response.builder()
.body("This is the about page")
.build())
)
.build()
)
.fallbackRoute(Route.builder()
.requestHandler(req -> ok(Response.builder()
.body("We could not find what you're looking for")
.build())
)
.build()
)
.build();
int port = 8000;
Application<Request, Response> app = Application.basic()
.port(port)
.router(router)
.build();
app.start();
Updated over 5 years ago