4

I have an existing application in Sping 3.0 that uses ControllerClassNameHandlerMapping to map Controller and methods such as:

StartController.class is mapped to http://127.0.0.1/app/start/*

then

StartController.class has a method called init() that is mapped to http://127.0.0.1/app/start/init.html

Here is my configuration:

@Bean
public ControllerClassNameHandlerMapping classNameControllerMappings() {
     return new ControllerClassNameHandlerMapping() {{
        setCaseSensitive(true);
        setDefaultHandler(new UrlFilenameViewController());
        setInterceptors(new Object[]
                {callProgressionInterceptorHandler(),
                 callSessionInterceptorHandler(),
                 localeChangeInterceptor()});
     }};
}

Most of my controllers have 5-15 Request Mapped methods in each controller.

But when I upgrade to Spring 3.1+, the Request Mapping becomes ambiguous for each controller and is not mapped correctly.

I have read that one solution is to explicitely add the mthod name:

@RequestMapping(method = RequestMethod.GET)

Will now be:

@RequestMapping(method = RequestMethod.GET, value = "init")

I really do not want to manually add @RequestMapping value to 100+ methods if I dont have to.

Can anyone help with a better solution?

Here is the error I keep getting:

        47672 [btpool0-1] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'addressConfirmationController' bean method
    public void com.comcast.ivr.d2.web.controllers.AddressConfirmationController.houseNumber_rc(org.springframework.ui.ModelMap)
    to {[],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'addressConfirmationController' bean method

I also added setOrder(1); to ControllerClassNameHandlerMapping and still get this error.

UPDATE: I saw on http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html the following excerpt:

Prior to Spring 3.1, type and method-level request mappings were examined in two separate stages -- a controller was selected first by the DefaultAnnotationHandlerMapping and the actual method to invoke was narrowed down second by the AnnotationMethodHandlerAdapter.

With the new support classes in Spring 3.1, the RequestMappingHandlerMapping is the only place where a decision is made about which method should process the request. Think of controller methods as a collection of unique endpoints with mappings for each method derived from type and method-level @RequestMapping information.

Does this mean I cannot keep the same @RequestMapping without adding mapping details in the @RequestMapping as I did <3.1 ?

I have hundreds of methods that would need to be modified in order for this to happen... :-(

Mick Knutson
  • 2,297
  • 3
  • 25
  • 48
  • Is the ambiguous mappings error reasonable? Do they make sense? – Sotirios Delimanolis Apr 16 '13 at 19:02
  • Yes they make sense, but adding the Request Mapping Value String to 100+ methods will require an enormous amount of regression testing which is too costly, and thus keeps us from upgrading to Spring 3.1+ – Mick Knutson Apr 16 '13 at 20:18
  • Have you got any other HandlerMappings in your application? If so, is one of them perhaps running before the ControllerClassNameHandlerMapping? What if you give ControllerClassNameHandlerMapping an order of 1? – Mark Chorley Apr 18 '13 at 20:15
  • So, It looks as if you should override DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter to get mappings by your ControllerClassNameHandlerMapping – kxyz Mar 09 '15 at 08:13
  • Can you provide a sample controller that has multiple handlers. Would like to see how you defined them. – praveenj Jun 24 '15 at 20:57

0 Answers0