Is it possible to dynamically set RequestMappings in Spring MVC?

Spring MVC performs URL mappings using implementations of the HandlerMapping interface. The ones usually used out of the box are the default implementations, namely SimpleUrlHandlerMapping, BeanNameUrlHandlerMapping and DefaultAnnotationHandlerMapping.

If you want to implement your own mapping mechanism, this is fairly easy to do – just implement that interface (or, perhaps more likely, extend AbstractUrlHandlerMapping), declare the class as a bean in your context, and it will be consulted by DispatcherServlet when a request needs to be mapped.

Note that you can have as many HandlerMapping implementations as you like in the one context. They will be consulted in turn until one of them has a match.

Leave a Comment