Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringBoot gets an error “There is already controller bean method”, when two get or two post methods gets added in controller

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'employeeController' method 
public java.util.List<com.test301.Test301.entity.Employee> com.test301.Test301.controller.EmployeeController.getLIstOfEmployees()
to {[],methods=[GET]}: There is already 'employeeController' bean method
public com.test301.Test301.entity.Employee com.test301.Test301.controller.EmployeeController.getEmployeeById(java.lang.Integer) mapped.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at com.test301.Test301.App.main(App.java:20) [classes/:na]
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'employeeController' method 
public java.util.List<com.test301.Test301.entity.Employee> com.test301.Test301.controller.EmployeeController.getLIstOfEmployees()
to {[],methods=[GET]}: There is already 'employeeController' bean method
public com.test301.Test301.entity.Employee com.test301.Test301.controller.EmployeeController.getEmployeeById(java.lang.Integer) mapped.
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:576) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:540) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:264) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:250) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:214) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:184) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:127) ~[spring-webmvc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    ... 16 common frames omitted

Contoroller class

/**
 * 
 */
package com.test301.Test301.controller;



import java.util.List;

import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test301.Test301.entity.Employee;
import com.test301.Test301.service.IEmployeeService;

@Controller
public class EmployeeController {

    @Autowired
    private IEmployeeService empService;

    @RequestMapping("/")
    public String getIndexPage() {
        return "index";
    }

    @ResponseBody
    @RequestMapping(name="/addEmployee", method = RequestMethod.POST)
    public String addEmployee(@RequestBody Employee emp) {
        return empService.addEmployee(emp);
    }

    @ResponseBody
    @RequestMapping(name="/getEmpList", method = RequestMethod.GET)
    public List<Employee> getLIstOfEmployees(){
        return empService.getListOfEmployees();
    }

    @ResponseBody
    @RequestMapping(name="/deleteEmployee/{id}", method = RequestMethod.DELETE)
    public String deleteEmployee(@PathParam("id") Integer empId) {
        return empService.deleteEmployee(empId);

    }

    @ResponseBody
    @RequestMapping(name="/getEmpById/{id}", method = RequestMethod.GET)
    public Employee getEmployeeById(@PathParam("id") Integer empId) {
        return empService.getEmployeeById(empId);
    }

    @ResponseBody
    @RequestMapping(name="/checkValidEmp", method = RequestMethod.POST)
    public String checkValidEmployee(@RequestBody Employee emp) {
        return empService.checkValidEmployee(emp);
    }

}

Here i am getting an bean there is already 'employeeContoroller' bean method exist. When I have added one get & one post method it worked properly but when i have added one extra get method i am getting above error even if both the url are different.same for post method also, with one post method it worked properly but with two post method it showed error.

like image 917
Prasant Senapati Avatar asked Feb 26 '19 12:02

Prasant Senapati


1 Answers

Change name with a path or value. Details of name, path, value

@RequestMapping(name="/addEmployee", method = RequestMethod.POST)

Correct:

@RequestMapping(path="/addEmployee", method = RequestMethod.POST)
like image 176
Romil Patel Avatar answered Sep 20 '22 22:09

Romil Patel