Configuring Spring MVC to map GET requests to one method in a controller and OPTIONS requests to another method

StackOverflow https://stackoverflow.com/questions/17489660

Question

This would be pretty easy using annotations:

@Controller
public class MyController {

  @RequestMapping(value="/hitmycontroller", method= RequestMethod.OPTIONS)
  public static void options(HttpServletRequest req,HttpServletResponse resp){
    //Do options
  }
  @RequestMapping(value="/hitmycontroller", method= RequestMethod.GET)
  public static void get(HttpServletRequest req,HttpServletResponse resp){
    //Do get
  }
}

but I can't find how to do this in XML. Is there some mapping handler that will do something like this:

<bean id="handlerMapping"
  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
      <mapping>
        <url>/hitmycontroller</url>
        <httpMethod>GET</httpMethod>
        <method>get</method>
        <controller>MyController</controller>
      </mapping>
      <mapping>
        <url>/hitmycontroller</url>
        <httpMethod>OPTIONS</httpMethod>
        <method>options</method>
        <controller>MyController</controller>
      </mapping>
  </property>
</bean>

Any pointers would be appreciated.

Was it helpful?

Solution

With the SimpleUrlHandlerMapping it is not possible specify the http method. Probably you have to use other mapping like the MethodUrlHandlerMapping in the Spring MVC REST project (http://spring-mvc-rest.sourceforge.net/).

The way to declare the mappings using the MethodUrlHandlerMapping should be something like this:

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="GET /hitmycontroller">MyController</prop>
            <prop key="OPTIONS /hitmycontroller">MyController</prop>
        </props>
    </property>
</bean>

You can see the example in their page:

http://spring-mvc-rest.sourceforge.net/introduction.html

Look at the part 2.

OTHER TIPS

Your @RequestMapping annotations should work. Just delete the handlerMapping bean from your xml configuration and enable MVC annotations.

Here is a sample configuration. Change base-package to the package that contain your controller classes

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <context:component-scan base-package="your.package" />
    <mvc:annotation-driven>
</beans>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top