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.
Executing a filtering query is easy… filtering well is difficult. Read the Beautiful Spark book if you want to learn how to create create data lakes that are optimized for performant filtering operations.
Apache Spark Spark filter () or where () function is used to filter the rows from DataFrame or Dataset based on the given one or multiple conditions or SQL expression. You can use where () operator instead of the filter if you are coming from SQL background. Both these functions operate exactly the same.
Spark filter () or where () function is used to filter the rows from DataFrame or Dataset based on the given one or multiple conditions or SQL expression. You can use where () operator instead of the filter if you are coming from SQL background. Both these functions operate exactly the same.
What is Spark Java? Spark is a Java micro framework that allows to quickly create web applications in Java 8. Spark is a lightweight and simple Java web framework designed for quick development. Sinatra, a popular Ruby micro framework, was the inspiration for it.
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 ...
);
}
}
The preferred manner to achieve what you are looking for would look like this.
get("/hello", (request, response) -> {
// look up your resource using resourceId in request/path/query
// oh dear, resource not found
throw new NotFoundException(resourceId);
});
exception(NotFoundException.class, (e, request, response) -> {
response.status(404);
response.body(String.format("Resource {%s} not found", e.getResourceId()));
// doReporting here.
});
public class NotFoundException extends Exception {
private final String resourceId;
public NotFoundException(String resourceId) {
this.resourceId = resourceId;
}
public String getResourceId() {
return resourceId;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With