Open cimi opened 3 years ago
I've made a wrapper for my handlers that sets the matched path as an attribute which I can read from other filters:
private static void get(String pathTemplate, Route handlerFn, PathParams ... pathParams) {
var path = buildMatchPath(pathTemplate, pathParams);
spark.Spark.get(path, wrap(path, handlerFn));
LOGGER.info("Registered GET handler for {}", path);
}
private static Route wrap(String matchedPath, Route handlerFn) {
return (request, response) -> {
request.attribute("matchedPath", matchedPath);
return handlerFn.handle(request, response);
};
}
I think this works, but it's hacky. It would be nice if request.matchPath() would be reliable, at least in the after handlers.
👋
I have a server with
before
,after
andexception
filters configured, through which I want to handle logging and metrics. I want to use the value ofmatchedPath
in these filters so I can avoid having repeated logging and metric code in my handlers.Unfortunately, I'm seeing that
request.matchedPath()
returns+/*paths
(SparkUtils.ALL_PATHS
) and not the path that matched inside these filters. I see the same output from all filters (before, after, exception), while the handler returns the correct path. Here's how my setup looks like (simplified):Is this a deliberate limitation or a bug? Is there a workaround?