我正在使用Spring MVC开发REST API。 它成功构建,但当我尝试将其部署到Tomcat 8.0.57时 它给出了以下错误,说我已经在该控制器中有一个bean方法。但我没有任何两个具有相同名称或RequestMethod的方法。
这是Apache Tomcat Log
29-Nov-2017 18:29:41.796 INFO [http-nio-8087-exec-72] org.apache.catalina.core.ApplicationContext.log 1 Spring WebApplicationInitializers detected on classpath
29-Nov-2017 18:29:41.844 INFO [http-nio-8087-exec-72] org.apache.catalina.core.ApplicationContext.log Initializing Spring root WebApplicationContext
29-Nov-2017 18:29:42.783 SEVERE [http-nio-8087-exec-72] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
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 'chatUserController' method
public java.lang.String org.test.chatapi.controller.ChatUserController.saveUser(org.test.chatapi.model.User)
to {[],methods=[POST]}: There is already 'chatMessageController' bean method
public java.lang.Integer org.test.chatapi.controller.ChatMessageController.saveMessage(org.test.chatapi.model.Message) mapped.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4736)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5181)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:586)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:460)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1586)
at sun.reflect.GeneratedMethodAccessor94.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1460)
at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:906)
at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:344)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:614)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'chatUserController' method
public java.lang.String org.test.chatapi.controller.ChatUserController.saveUser(org.test.chatapi.model.User)
to {[],methods=[POST]}: There is already 'chatMessageController' bean method
public java.lang.Integer org.test.chatapi.controller.ChatMessageController.saveMessage(org.test.chatapi.model.Message) mapped.
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:570)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:534)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:258)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:244)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:214)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:184)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:124)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 59 more
29-Nov-2017 18:29:42.789 INFO [http-nio-8087-exec-72] org.apache.catalina.core.ApplicationContext.log Closing Spring root WebApplicationContext
这些是控制器。 ChatUserController
@RestController
public class ChatUserController {
// Store the Users temporarily
ArrayList<User> users = new ArrayList<>();
// Get all the online users
@RequestMapping(name = "/users/", method = RequestMethod.GET)
public ArrayList<User> getUsers(){
users.add(new User("Janith","Hellow worlds"));
return users;
}
// Save new user / User Login
@RequestMapping(name = "/users/",method = RequestMethod.POST)
public String saveUser(@RequestBody User user){
if(user != null){
users.add(user);
return "Successfull Saved";
}else{
return "Failed";
}
}
// Remove the user / User sign out
@RequestMapping(name = "/users/{uname}", method = RequestMethod.DELETE)
public String deleteUser(@PathVariable String uname){
for(User u:users){
if(u.getUsername().equals(uname)){
users.remove(u);
return "Removed Successfully";
}
}
return "User Not found. Should be already Deleted.";
}
}
ChatMessagesController
@RestController
public class ChatMessageController {
ArrayList<Message> messagesList = new ArrayList<>();
@RequestMapping(name = "/messages/{username}", method = RequestMethod.PUT)
public ArrayList<Message> getChat(@PathVariable String username) {
// create a new list of unread messages
ArrayList<Message> myMessages = new ArrayList<>();
// add the unread messages to list
for (Message m : messagesList) {
if (m.getReciever().equals(username)) {
if (!m.isViewed()) {
m.setViewed(true);
myMessages.add(m);
}
}
}
return myMessages;
}
@RequestMapping(name = "/messages/", method = RequestMethod.POST)
public Integer saveMessage(@RequestBody Message message){
if(message != null){
message.setViewed(false);
messagesList.add(message);
return new Integer(1);
}else{
return new Integer(0);
}
}
}
但是这个ChatUserController运行良好,我部署并测试了它。 添加ChatMessageController的第二个方法saveMessage()后会出现此错误。当第一个方法getChat()在GET RequestMethod中时,也会出现同样的错误。我把它改成PUT方法后修复了。 我是Spring MVC Web API的新手,所以我对此有所帮助。