Before
filters are evaluated before each request and can read the request and
read/modify the response.
import static spark.Spark.*; public class HelloSpark { public static void main(String[] args) { before((request, response) -> { System.out.println("I am in before filter"); }); get("/hello", (request, response) -> { System.out.println("I am processing"); return "Hello"; }); after((request, response) -> { System.out.println("I am in after filter"); }); } }
Hit
above URL, you will get below messages in console.
I
am in before filter
I
am processing
I
am in after filter
Filters
optionally take a pattern, causing them to be evaluated only if the request
path matches that pattern.
before("/hi", (request, response) -> { System.out.println("Filter for request hi"); });
Above
filter evaluates, if request url is "/hi" (you can add wildcards to
url).
import static spark.Spark.*; public class HelloSpark { public static void main(String[] args) { before((request, response) -> { System.out.println("I am in before filter"); }); get("/hello", (request, response) -> { System.out.println("I am processing hello request"); return "Hello"; }); after((request, response) -> { System.out.println("I am in after filter"); }); before("/hi", (request, response) -> { System.out.println("Filter for request hi"); }); get("/hi", (request, response) -> { System.out.println("I am processing hi request"); return "Hi"; }); } }
Hit
URLs
You
will get messages like below in console.
I
am in before filter
I
am processing hello request
I
am in after filter
I
am in before filter
Filter
for request hi
I
am processing hi request
I
am in after filter
No comments:
Post a Comment