HandlerFilter is analogous to the Servlet filter. This executes before the request gets processed by HandlerFunction. There could be chain filters that get executed before the request gets served. If a filter sends back a ServerResponse, then the request is terminated as follows:
helloRoute.filter((request, next) -> { if (request.headers().acceptCharset().contains(Charset.forName("UTF-8"))) { return next.handle(request); } else { return ServerResponse.status(HttpStatus.BAD_REQUEST).build(); }});
The preceding code is doing the following:
- Adding a filter to helloRoute using the filter() method
- The filter takes a request and the next handler function
- Validating whether or not the request headers contain the UTF-8 charset in ...