gpt4 book ai didi

Spring Boot 收到错误 “There is already controller bean method” ,当在 Controller 中添加两个 get 或两个 post 方法时

转载 作者:行者123 更新时间:2023-12-03 22:10:09 24 4
gpt4 key购买 nike

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

Controller 类
/**
*
*/
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 方法。当我添加了一个 get 和一个 post 方法时,它可以正常工作,但是当我添加了一个额外的 get 方法时,即使两个 url 不同,我也会出现上述错误。 post 方法也相同,使用一种 post 方法它可以正常工作,但与两个 post 方法它显示错误。

最佳答案

使用路径或值更改名称。 Details of name, path, value

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

正确的:
@RequestMapping(path="/addEmployee", method = RequestMethod.POST)

关于Spring Boot 收到错误 “There is already controller bean method” ,当在 Controller 中添加两个 get 或两个 post 方法时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54885516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com