【问题标题】:Configuring Spring MVC to map GET requests to one method in a controller and OPTIONS requests to another method配置 Spring MVC 以将 GET 请求映射到控制器中的一个方法,并将 OPTIONS 请求映射到另一种方法
【发布时间】:2013-07-03 14:27:54
【问题描述】:

使用注释会很容易:

@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
  }
}

但我找不到如何在 XML 中执行此操作。是否有一些映射处理程序会做这样的事情:

<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>

任何指针将不胜感激。

【问题讨论】:

    标签: java spring http spring-mvc http-options-method


    【解决方案1】:

    使用 SimpleUrlHandlerMapping 无法指定 http 方法。可能您必须使用其他映射,例如 Spring MVC REST 项目中的 MethodUrlHandlerMapping (http://spring-mvc-rest.sourceforge.net/)。

    使用 MethodUrlHandlerMapping 声明映射的方式应该是这样的:

    <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>
    

    您可以在他们的页面中看到示例:

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

    请看第 2 部分。

    【讨论】:

      【解决方案2】:

      您的 @RequestMapping 注释应该可以工作。只需从您的 xml 配置中删除 handlerMapping bean 并启用 MVC 注释。

      这是一个示例配置。 将 base-package 更改为包含您的控制器类的包

      <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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多