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

2

9
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.

Heterochromous answered 26/2, 2019 at 12:23 Comment(3)
You are using name where you probably wanted to use value . Currently each get maps to the same URL.Sulfaguanidine
And you further could probably use @RestController instead of @Controller and then remove @ResponseBody from all methods too...Selie
Thank You very much. It resolved my error.Heterochromous
M
11

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)
Mancunian answered 26/2, 2019 at 14:0 Comment(0)
C
1

if you have multiple controllers let's say one Role has DeleteMapping with value = "delete" and in another controller for example named Person which also has DeleteMapping with value = "delete" , then you will have this error like in mine case. You can just change VELUE to something else and it will work

Censorship answered 16/12, 2022 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.