Reputation: 137
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.
Upvotes: 1
Views: 1441
Reputation: 17361
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>
Upvotes: 0
Reputation: 1192
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.
Upvotes: 1