Q:
Is it possible to somehow get a hold of a @PathVariable in a @ControllerAdvice given that the PathVariable is only present in some requests?
If this was in a Controller, I could write 2 different Controllers. But a ControllerAdvice always applies to all requests. I can't have a ControllerAdvice apply to only Controllers where the PathVariable is defined.
A:
You can inject the Map of path variables and check for the existence of a key.
public void advise(@PathVariable Map<String, String> pathVariables) {
if (pathVariables.containsKey("something")) {
String something = pathVariables.get("something");
// do something
} else {
// do something else
}
}