Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC 3 : Ambiguous mapping found

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

like image 444
Amit Patel Avatar asked Jan 18 '12 11:01

Amit Patel


People also ask

What is ambiguous mapping in Spring Boot?

Ambiguous Mapping Error The ambiguous mapping error occurs when Spring evaluates two or more request mappings to be the same for different controller methods. A request mapping is the same when it has the same HTTP method, URL, parameters, headers, and media type.

When is a request mapping ambiguous?

A request mapping is the same when it has the same HTTP method, URL, parameters, headers, and media type. For example, this is an ambiguous mapping: The exception thrown usually does have error messages along these lines:

Does Spring MVC handle default method requests without explicit path mapping?

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.

What is the use of annotation in Spring MVC?

Simply put, the annotation is used to map web requests to Spring Controller methods. How to map and handle static resources with Spring MVC - use the simple configuration, then the 3.1 more flexible one and finally the new 4.1 resource resolvers. Learn how to work with forms using Spring MVC - mapping a basic entity, submit, displaying errors.


2 Answers

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.

like image 178
Arun P Johny Avatar answered Oct 01 '22 20:10

Arun P Johny


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.

like image 26
Rossen Stoyanchev Avatar answered Oct 01 '22 21:10

Rossen Stoyanchev