Spring MVC 3 : Ambiguous mapping found

筅森魡賤 提交于 2019-12-04 16:29:39

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/8909482/spring-mvc-3-ambiguous-mapping-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!