15

I am playing with spring MVC 3.1 and testing different features. I wanted to verify following statement taken from @RequestMapping#value doc

If you have a single default method (without explicit path mapping), then all requests without a more specific mapped method found will be dispatched to it. If you have multiple such default methods, then the method name will be taken into account for choosing between them

So I created following controller with multiple default handler methods.

@Controller
@RequestMapping("/book")
public class BookController {

    @RequestMapping
    public @ResponseBody String greet() {
        return "Hi Book!";
    }

    @RequestMapping
    public @ResponseBody String meet() {
        return "Nice to meet you Book!";
    }
}

Here is web application context configuration

<beans ....>
<!-- Use @Component annotations for bean definitions -->
  <context:component-scan base-package="com.botreeconsulting.lms.web"/>

  <!-- Use @Controller annotations for MVC controller definitions -->
  <mvc:annotation-driven />

  <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
   </bean>

</beans>

But it seems I messed up something as it is giving me following error at the time of deployment:

java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'bookController' bean method 
public java.lang.String com.botreeconsulting.lms.web.BookController.meet()
to {[/book],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'bookController' bean method
public java.lang.String com.botreeconsulting.lms.web.BookController.greet() mapped.

Now the question is does this controller models what is written in the document? I feel that I didn't get it properly. Please guide me to model the controller to match the statement about multiple default handlers.

Thanks, Amit

Amit Patel
  • 15,609
  • 18
  • 68
  • 106

2 Answers2

26

If you have a controller as given below, all requests other than /book/edit will be directed to mydefault() while /book/edit will be sent to meet().

@Controller
@RequestMapping("/book")
public class BookController {

    @RequestMapping
    public @ResponseBody String mydefault() {
        return "Hi Book!";
    }

    @RequestMapping("/edit")
    public @ResponseBody String meet() {
        return "Nice to meet you Book!";
    }
}

In your sample you have two methods without explicit path mapping.

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • `mydefault()` will handle /book only not /book/abc. – Amit Patel Jan 18 '12 at 13:06
  • Arun, `If you have multiple such default methods...`. Is it possible to configure multiple default methods? – Amit Patel Jan 23 '12 at 07:59
  • What do you mean by multiple defaults? In any design there will be only one default – Arun P Johny Jan 23 '12 at 10:10
  • 1
    Arun, I understand that there is only single default possible in a controler. Now how come this Spring document comment says `If you have a single default method (without explicit path mapping), then all requests without a more specific mapped method found will be dispatched to it. If you have multiple such default methods, then the method name will be taken into account for choosing between them`? – Amit Patel Jan 23 '12 at 10:29
  • I think what it means is if you have more than 1 method without the url mapping(ie with only `@RequestMappin` no path speficied` then the name of the method will be considered. Assume that to the above example you add a method named `add` with annotation `@RequestMapping` and then you request `/book/add` then the `add` method will be called. – Arun P Johny Jan 23 '12 at 10:59
  • Exactly, that's what I wanted to check ahd hence I created the example with multiple methods with @ReqeustMapping(without explicit path) and ended with ambiguity error. All I wanted is to validate that it is not possible to have multiple default request mapping methods even though it is stated in the document. – Amit Patel Jan 23 '12 at 11:35
  • Can you share your spring mvc configuration? Are you using an `RequestMappingHandlerMapping` or `DefaultAnnotationHandlerMapping` – Arun P Johny Jan 23 '12 at 13:34
  • You can have a look at the answer provided by Rossen Stoyanchev also. – Arun P Johny Jan 24 '12 at 03:44
  • I haven't specified `HandlerMapping` explicitly so it would be `DefaultAnnotationHandlerMapping` as Rossen Stoyanchev posted. For reference I have added mvc configuration in original post – Amit Patel Jan 24 '12 at 08:21
5

Arun, your answer is correct with the caveat that in Spring 3.1 it depends which HandlerMapping-HandlerAdapter pair is configured.

The described behavior is supported with the DefaultAnnotationHandlerMapping & AnnotationMethodHandlerAdapter which have been in use since Spring 2.5 and are still enabled by default when no other HandlerMapping and HandlerAdapter beans are defined.

The RequestMappingHandlerMapping and RequestMappingHandlerAdapter added in Spring 3.1 (see Spring 3.1 reference docs) as a replacement for the former do not support the same behavior -- i.e. falling back on the method name in case of ambiguous mappings as well as having a default method (when no explicit mappings are defined). The new HandlerMapping-HandlerAdapter pair is enabled by default from the MVC namespace and from MVC Java config and is recommended for use going forward.

The Java doc referenced by Arun needs an update. I've created a ticket for that SPR-9042.

Rossen Stoyanchev
  • 4,910
  • 23
  • 26
  • Hi, Rossen. Can you please clarify a bit the statement `enabled by default from the MVC namespace` - do you mean `` tag, or the tag registers just another pair of HandlerMapping/HandlerAdapter? – Boris Treukhov Dec 03 '12 at 11:15
  • Also, can you please look at http://stackoverflow.com/questions/13671776/spring-mvc-why-this-hello-world-run-well-without-annotation-driven-tag-unlike-a - in this question the controller is annotated only with '@Controller' while its single method is annotated '@RequestMapping("/hello")' am I understanding correctly that this configuration will work only in pre 3.1 versions(without `` tag) because of the changes in Spring 3.1? – Boris Treukhov Dec 03 '12 at 11:16