1

Since Spring has deprecated DefaultAnnotationHandlerMapping and has been replaced by RequestMappingHandlerMapping, I'm stuck with a case where I had code like below:

@Autowired
private DefaultAnnotationHandlerMapping handlerMapping;

//in an API that lists handler mapping
Map<String, Object> handlerMap = handlerMapping.getHandlerMap();

Now, RequestMappingHandlerMapping doesn't provide this method. Which function can I use to get the desired handler map?

Things to consider while answering:

  • I have limited knowledge about how handler mapping works basically limited to what is explained here.

Things I have been looking at:

  • I tried to go through the parent classes for both DefaultAnnotationHandlerMapping & RequestMappingHandlerMapping. I reached the conclusion that AbstractUrlHandlerMapping is the function that contains getHandlerMap() and extends AbstractHandlerMapping.

  • RequestMappingHandlerMapping has AbstractHandlerMethodMapping<T> as a super parent and extends AbstractHandlerMapping but I couldn't find something similar to getHandlerMap()

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Vivek Shankar
  • 770
  • 1
  • 15
  • 37

1 Answers1

0

Turns out since AbstractUrlHandlerMapping is the class that holds the function and getHandlerMap going by it's javadoc

Return the registered handlers as an unmodifiable Map, with the registered path as key and the handler object (or handler bean name in case of a lazy-init handler) as value.

Since this is an internal Spring map any class that extends AbstractUrlHandlerMapping can be used to retrieve the map. Specifically, I used BeanNameUrlHandlerMapping.

Vivek Shankar
  • 770
  • 1
  • 15
  • 37
  • This will give you an empty map. It contains only the mappings for the given `HandlerMapping` not all the URLs. – M. Deinum May 08 '19 at 09:51
  • @M.Deinum I couldn't understand what you mean by given `HandlerMapping`. I don't pass a handler mapping anywhere, do you mean spring internally uses the class(e.g. `BeanNameUrlHandlerMapping`) to populate the handler map? Could you please elaborate. – Vivek Shankar May 08 '19 at 12:21
  • 1
    You answered your question by stating you used `BeanNameUrlHandlerMapping`. If you inject a `HandlerMapping` you will only be able to retrieve the mappings internal to that `HandlerMapping` not all available mappings. The `BeanNameUrlHandlerMapping` will NOT contain the mappings detected by the `RequestMappingHandlerMapping`... – M. Deinum May 08 '19 at 12:27
  • Alright, but then how do I access the mappings for `RequestMappingHandlerMapping`? Since it doesn't provide the `getHandlerMap()` function. – Vivek Shankar May 09 '19 at 05:12
  • The quick answer, you don't as the mappings are internal to that class instance. The long answer you could use reflection to pull out the mappings. – M. Deinum May 09 '19 at 05:24
  • @M.Deinum Could you point me to a link doing something similar to the long answer you mentioned above? – Vivek Shankar May 09 '19 at 06:13
  • There is no real answer, it was mere a figure of speech. – M. Deinum May 09 '19 at 06:36