Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-6822

Having GET and POST methods for the same URL on different classes causes error when using annotation-driven

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 3.0 GA
    • Fix Version/s: 3.1 M2
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      Given the following:

      Controller1.java
      @Controller
      public class Controller1 {
        @RequestMapping(value="/someurl", method=GET)
        public String doGet() {
          return "view";	
        }	
      }
      
      Controller2.java
      @Controller
      public class Controller2 {
        @RequestMapping(value="/someurl", method=POST)
        public String doPost() {
          // do something different for posts	
          return "view";	
        }	
      }
      
      web-application-config.xml
      ....
          <context:spring-configured/>
          <context:annotation-config/>
          <context:component-scan base-package="..."/>
          <context:component-scan base-package="..."/>
      ....
      

      you get the following exception:

      org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0' defined in ServletContext resource [/WEB-INF/web-application-config.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot map handler [controller2] to URL path [/someurl]: There is already handler [[email protected]] mapped.
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
      	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
      	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
      	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
      	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
      	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:557)
      	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)
      	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:416)
      	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:443)
      	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:459)
      	at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:340)
      	at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:307)
      	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
      ...
      

      In our specific domain it makes most sense to have the GET and POST for the same URL split over two different classes.

        Attachments

          Activity

            People

            • Assignee:
              rstoya05-aop Rossen Stoyanchev
              Reporter:
              markhopkins Mark Hopkins
              Last updater:
              Trevor Marshall
            • Votes:
              2 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                7 years, 19 weeks, 5 days ago