过滤器未被调用。

时间:2014-12-12 14:58:41

标签: spring model-view-controller filter cors

所以我编写了一个看起来有点像这样的Spring REST应用程序:

web.xml中:     

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Archetype Created Web Application</display-name>

<filter>
    <filter-name>CORSFilter</filter-name>
    <filter-class>com.billboard.filters.SimpleCORSFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>CORSFilter</filter-name>
    <url-pattern>/</url-pattern>
  </filter-mapping>

  <servlet>
      <servlet-name>mvc-dispatcher</servlet-name>
      <servlet-class>
          org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>mvc-dispatcher</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

MVC-调度-servlet.xml中:

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

    <context:component-scan base-package="com.billboard.imageService"/>

    <mvc:annotation-driven/>

</beans>

,控制器如下所示:     包com.billboard.imageService;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.billboard.rangeCache.Cache;
import com.billboard.rangeCache.CacheManager;
import com.billboard.rangeCache.ICache;

@Controller
public class ImageController {

    @RequestMapping("/get")
    public @ResponseBody String test() {
        System.out.println("get images");
        return "okay";
    }

    @RequestMapping("/getImages/{start}/{size}")
    public @ResponseBody List<Image> getImages(@PathVariable String start, @PathVariable String size) {
        System.out.println("get images");
        ICache<Image> cache = CacheManager.getCache("imageCache");
        return cache.get(Integer.parseInt(start), Integer.parseInt(size));
    }
}

问题是Filter永远不会被调用。我在这里做错了吗? Spring MVC会干扰容器处理过滤器的方式吗? (我非常怀疑)。我所要做的就是确保REST服务实现COR(交叉源请求)。

1 个答案:

答案 0 :(得分:1)

您可以尝试在网址映射中添加*

 <filter-mapping>
    <filter-name>CORSFilter</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping>

如果不是,我认为它只会服务于&#34; root&#34;没有别的。

  

如果要将每个请求记录到Web应用程序,您可以将命中计数器过滤器映射到URL模式/ *。

Here is a link to filters以上引用来自。