2

I couldn't understand why the following gives an ambiguous mapping.

a. The controller class

@Controller
@RequestMapping("/employees")
public class EmployeeController {

@Autowired
private EmployeeService employeeService;

@RequestMapping(value = "/new", method = RequestMethod.GET)
public String get(Model model) {
    return "xx";
}

}

Console output:

java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'org.tutorial.spring.controller.EmployeeController#0' bean method 
public java.lang.String org.tutorial.spring.controller.EmployeeController.get(org.springframework.ui.Model)
to {[/employees/new],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'employeeController' bean method
public java.lang.String org.tutorial.spring.controller.EmployeeController.get(org.springframework.ui.Model) mapped.    

It says method has already been mapped. How so?

b. web.xml

<servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

c. spring.xml

<context:annotation-config />
<context:component-scan base-package="org.tutorial.spring" />
<mvc:annotation-driven />
<bean class="org.tutorial.spring.controller.EmployeeController" />

1 Answer 1

7

Try removing the bean element

<bean class="org.tutorial.spring.controller.EmployeeController" />

from your XML. I suspect this is causing you problems because

<context:component-scan base-package="org.tutorial.spring" />

will scan your package, and identify EmployeeController as a bean, due to its annotations. Then, afterwards, you are manually adding the same bean.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.