当前位置: 首页 > 知识库问答 >
问题:

当在控制器中添加两个get或两个post方法时,SpringBoot会收到一个错误“There ready is controller bean method”

微生恩
2023-03-14
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

控制

/**
 * 
 */
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);
    }

}

这里我得到了一个bean,已经有了“employeeContoroller”bean方法。当我添加了一个

共有1个答案

经景辉
2023-03-14

使用路径或值更改名称。名称、路径、值的详细信息

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

正确:

@RequestMapping(path="/addEmployee", method = RequestMethod.POST)
 类似资料:
  • 下面的代码是一个按钮的方法。 它总是给我一个错误。 如果我删除,那么它可以正常工作。 当我单击按钮时,如何使用POST方法,并移动到另一个活动? 错误:04-27 19:41:26.066 197 4-1974/com.opshun_test.opshun_test e/androidruntime:致命异常:main process:com.opshun_test.opshun_test,pid

  • 问题内容: 我在一个视图控制器中有两个S。我可以让一个工作,但是当我添加另一个工作时,我的应用程序崩溃了。这是我用于一个选择器视图的代码: 现在,如何让第二个选择器工作?假设我的第二个选择器视图称为(另一个称为)。我尝试在选择器视图方法中复制代码,但它不起作用。 问题答案: 根据我在问题中得到的信息,我想您需要设置数据源和委托方法来处理区分哪个选择器实例正在调用它们的能力。 在选择器视图上使用ta

  • 我使用Java11和Spring。我试图通过post将两个对象发送到控制器,但我得到了一个错误。 这里是控制器: 你知道为什么我会得到这个错误以及如何修复它吗?

  • 我试图测试两个不同的控制器类,因为我想测试两个类中的每个方法,我将使用@WebMvcTest,我的问题是有一种方法可以将模拟注入到两个类中,可能是这样的? 当然,这会产生错误,所以这是否意味着当使用@WebMvcTest时,我们只能在一个控制器中测试方法?每班

  • 这么低的方法如下,有更好的建议吗?