3

How to map requests to methods without explicit annotations on top of methods? For instance, the following request:

http://somedomain:8080/SampleSpring/access/loginFailed

should be mapped to

"loginFailed" method of "AccessController" without the need of explicit annotation on method like:

@RequestMapping("/access/loginFailed")

Here is my spring configuration:

<context:component-scan base-package="com.robikcodes.samplespring"/>
<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
        <property name="basePackage" value="com.robikcodes.samplespring.controller"/>
        <property name="caseSensitive" value="true"/>
        <property name="defaultHandler">
            <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
        </property>
</bean>   

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
</bean>

Here is my controller:

@Controller
public class AccessController{

    @RequestMapping(method = RequestMethod.GET)
    public void login(ModelMap m) {}

    @RequestMapping(method = RequestMethod.GET)
    public String loginFailed(ModelMap m) {
        m.addAttribute("error", "true");
        return "access/login";
    }

    @RequestMapping(method = RequestMethod.GET)
    public String logout(ModelMap m) {
        m.addAttribute("logoutStatus","true");
        return "access/login";
    }
}

I got the following error (seems like only login method was mapped properly):

org.apache.catalina.LifecycleException: 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 'accessController' bean method 
public java.lang.String com.robikcodes.samplespring.controller.AccessController.logout(org.springframework.ui.ModelMap)
to {[],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'accessController' bean method
public void com.robikcodes.samplespring.controller.AccessController.login(org.springframework.ui.ModelMap) mapped.
nobeh
  • 9,784
  • 10
  • 49
  • 66
coolscitist
  • 3,317
  • 8
  • 42
  • 59
  • 2
    Although a different question, but [this answer](http://stackoverflow.com/a/8909851/248082) also applies to your question. – nobeh Dec 23 '13 at 09:29
  • Yeah it does. I do not know how to use DefaultAnnotationHandlerMapping & AnnotationMethodHandlerAdapter instead of RequestMappingHandlerMapping and RequestMappingHandlerAdapter. Even if I do replace then still they are deprecated. Does this mean there is no way to use method names without using deprecated classes? – coolscitist Dec 23 '13 at 10:20
  • In fact [`RequestMappingHandlerAdapter`](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.html) is not deprecated and you should use it and it also supports full method signatures. – nobeh Dec 23 '13 at 10:26
  • I meant DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter are deprecated. In the link you pointed, Rossen says: "The new HandlerMapping-HandlerAdapter pair is enabled by default". Then why is my code not working? Do I need to declare RequestMappingHandlerAdapter explicitly? If so how? – coolscitist Dec 23 '13 at 10:38

1 Answers1

5

You're using ControllerClassNameHandlerMapping with an assumption that is not correct; from Java doc:

Implementation of HandlerMapping that follows a simple convention for generating URL path mappings from the class names of registered Controller beans as well as @Controller annotated beans.

The documentation does not say that it also follows method names. The main reference of comparing "handler mappings" for your controller is the @RequestMapping annotations put on your methods. So, with your controller Spring reads them as:

{methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}

for all the defined methods in AccessController that has the following @RequestMapping:

@RequestMapping(method = RequestMethod.GET)

That's why you see the ambiguous exception.

To my understanding, the cleanest solution is to use value attribute of @RequestMapping to define different request URIs. It's not really recommended to go for a solution that tries to map request URIs to method names.

nobeh
  • 9,784
  • 10
  • 49
  • 66