I'm running Spring MVC 3.x and I have one Controller with RequestMapping annotations (DefaultAnnotationHandlerMapping) and I have one ServletForwardingController with some additional mappings via SimpleUrlHandlerMapping. When I boot up my application, I see the following:
...
13:24:17,747 INFO [DefaultAnnotationHandlerMapping] Mapped URL path [/{query}] onto handler [com.foo.controllers.BarController@1b20a36]
13:24:17,997 INFO [SimpleUrlHandlerMapping] Root mapping to handler [org.springframework.web.servlet.mvc.ParameterizableViewController@598535]
13:24:18,044 INFO [SimpleUrlHandlerMapping] Mapped URL path [/spring*.ftl] onto handler [org.springframework开发者_C百科.web.servlet.mvc.ServletForwardingController@56eb62]
13:24:18,044 INFO [SimpleUrlHandlerMapping] Mapped URL path [/shared-resources/**] onto handler [org.springframework.web.servlet.mvc.ServletForwardingController@56eb62]
...
My BarController is of course catching all requests (like /spring_en_US.ftl), but I want it to be tried last. In other words, I want the SimpleUrlHandlerMapping to take priority over the DefaultAnnotationHandlerMappings in my application.
The best solution I found was to replace mvc:annotation-config with explicit DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter bean configuration. I was then able to set the order property on both SimpleUrlHandlerMapping (order = 0) and DefaultAnnotationHandlerMapping (order = 1). This set the priority of the handlers as I needed.
I tried to reorder mvc:annotation-config below SimpleUrlHandlerMapping, but it didn't change the handler priority as needed.
DispatcherServlet
should consult the various HandlerMapping
beans in the order in which they're declared in the beans file. The first one that says "yes, I have a mapping for that" gets the prize.
If you're manually declaring a SimpleUrlHandlerMapping
and DefaultAnnotationHandlerMapping
, then make sure they're in the right order.
If you're not declaring them yourself, then Spring will use the defaults, which is a BeanNameUrlHandlerMapping
followed by a DefaultAnnotationHandlerMapping
.
精彩评论