Using spring DefaultAnnotationHandlerMapping how can I lookup the Controller that would ultimately handle a given url.
I currently have this, but feels like there ought to be a cleaner way than iterating over 100s of request mappings:
public static Object getControllerForAction(String actionURL) {
ApplicationContext context = getApplicationContext();
AnnotationHandlerMapping mapping = (AnnotationHandlerMapping) context.getBean("annotationMapper");
PathMatcher pathMatcher = mapping.getPathMatcher();
for (Object key: mapping.getHandlerMap().keySet()) {
if (pathMatcher.match((String) key, actionURL)){
return mapping.getHandlerMap().get(key);
}
}
return null;
}
For the purposes of this question, all of the interesting methods in
DefaultAnnotationHandlerMappingand its superclasses are protected, and so not visible to external code. However, it would be trivial to write a custom subclass ofDefaultAnnotationHandlerMappingwhich overrides these methods and makes thempublic.Since you need to be able to supply a path rather than a request object, I would suggest
lookupHandlerofAbstractUrlHandlerMappingwould be a good candidate for this. It still needs you to supply it with a request object as well as the path, but that request object is only used to pass to the validateHandler() method, which does nothing, so you could probably supply a null there.