Spring MVC 3 : Ambiguous mapping found

后端 未结 2 1036
误落风尘
误落风尘 2021-02-13 03:35

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         


        
相关标签:
2条回答
  • 2021-02-13 03:49

    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.

    0 讨论(0)
  • 2021-02-13 04:06

    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.

    0 讨论(0)
提交回复
热议问题