Use Java Spark after filter to run custom action when a 404 happens

java, spark-java

Solution

I solved this one using wildcard routes. Instead of calling the `after` method, I added a route for each of the HTTP methods that binds the "*" route.

It's important to have them at the bottom of the your `Main` method so if no route gets resolved these ones always get triggered.

Here's an example:

import static spark.Spark.*;

public class MyApp {
    public static void main(String[] args) {
        get("/hello", (req, res) -> "{\"status\":\"OK\"}");

        get("*", (request, response) -> {
            System.out.println("404 not found!!");
            // email me the request details ...    
        );
    }
}

Problem

I'll try to keep it short. Here's the problem I'm having while trying to understand Spark filters. I'm trying to create a simple app and one of the things that it should do is to create an error report every time the client is about to see an http error e.g. 404 or 500. Here's how my app looks like: ``` import static spark.Spark.*; public class MyApp { public static void main(String[] args) { get("/hello", (req, res) -> "{\"status\":\"OK\"}"); after((request, response) -> { if (response.raw().getStatus() == 404) { // here run the code that will report the error e.g. System.out.println("An error has occurred!!"); } }); } } ``` For some reason, the `response` parameter has its status attribute set to 0 when I'm checking if it's set to 404. The documentation says `"after" filters are evaluated after each request and can read the request and read/modify the response` so, I should be able to do it somehow (unless the docs are wrong). Basically, I'm trying to intercept http errors using an `after` filter but when I try to check the response I don't get what I expect. Has anyone an idea what would be a different way of doing the same thing or how to make this work? Thanks.

Original source

Related problems