Spring 3.0:处理程序映射问题

时间:2010-03-15 16:20:24

标签: spring-mvc spring

我无法将特定的URL请求映射到项目中的某个控制器。

网址为:http://HOSTNAME/api/v1/profiles.json 部署的战争是:api.war

我得到的错误如下:

  

[PageNotFound]找不到映射   带URI的HTTP请求   [/api/v1/profiles.json] in   DispatcherServlet,名称为“action”

我的配置如下:web.xml:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-security.xml</param-value>
 </context-param>

 <!-- Cache Control filter -->
 <filter>
  <filter-name>cacheControlFilter</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
 </filter>

 <!-- Cache Control filter mapping -->
 <filter-mapping>
  <filter-name>cacheControlFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 <!-- Spring security filter -->
 <filter>
  <filter-name>springSecurityFilterChain</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
 </filter>

 <!-- Spring security filter mapping -->
 <filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 <!-- Spring listener -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <!-- Spring Controller -->
 <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>/v1/*</url-pattern>
 </servlet-mapping> 

action-servlet.xml:

<mvc:annotation-driven/>    

<bean id="contentNegotiatingViewResolver"
  class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <property name="favorPathExtension" value="true" />
  <property name="favorParameter" value="true" />
  <!--
   default media format parameter name is 'format'
  -->
  <property name="ignoreAcceptHeader" value="false" />
  <property name="order" value="1" />
  <property name="mediaTypes">
      <map>
        <entry key="html" value="text/html"/> 
        <entry key="json" value="application/json" />
        <entry key="xml"  value="application/xml" /> 
      </map>
  </property>  
  <property name="viewResolvers">
      <list>        
        <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/WEB-INF/jsp/"/>
          <property name="suffix" value=".jsp"/>
          <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        </bean>
      </list>
  </property>  
  <property name="defaultViews">
      <list>              
        <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
        <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
          <constructor-arg>
            <bean class="org.springframework.oxm.xstream.XStreamMarshaller" />
          </constructor-arg>
        </bean>        
      </list>
  </property>
 </bean>

应用程序上下文安全性:

<sec:http auto-config='true'  >
  <sec:intercept-url pattern="/login.*"      filters="none"/> 
  <sec:intercept-url pattern="/oauth/**"       access="ROLE_USER" />
  <sec:intercept-url pattern="/v1/**"       access="ROLE_USER" />
  <sec:intercept-url pattern="/request_token_authorized.jsp"  access="ROLE_USER" />
  <sec:intercept-url pattern="/**"        access="ROLE_USER"/>
  <sec:form-login authentication-failure-url ="/login.html"
      default-target-url   ="/login.html" 
      login-page     ="/login.html"
      login-processing-url  ="/login.html" />

  <sec:logout logout-success-url="/index.html" logout-url="/logout.html" />
 </sec:http>

控制器:

@Controller
public class ProfilesController {

 @RequestMapping(value = {"/v1/profiles"}, method = {RequestMethod.GET,RequestMethod.POST})
 public void getProfilesList(@ModelAttribute("response") Response response) {
  ....
 }

}

请求永远不会到达此控制器。

有什么想法吗?

1 个答案:

答案 0 :(得分:5)

注释在处理之前不会执行任何操作。你需要

<context:component-scan base-package="path.to.controllers"/>

作为root“beans”标记的子代,以便让Spring扫描控制器。 Spring将扫描base-package中的控制器及其后代。

此标记需要

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"

在您的根“beans”标记中。