【问题标题】:Filter not getting invoked.过滤器没有被调用。
【发布时间】:2023-03-13 18:51:01
【问题描述】:

所以我写了一个看起来有点像这样的 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-dispatcher-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));
    }
}

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

【问题讨论】:

    标签: spring model-view-controller filter cors


    【解决方案1】:

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

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

    如果不是,我认为它只会服务于“根”而没有别的。

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

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

    【讨论】:

      猜你喜欢
      • 2012-07-19
      • 1970-01-01
      • 2017-08-12
      • 1970-01-01
      • 2013-04-13
      • 2019-08-21
      • 1970-01-01
      • 2013-12-07
      • 1970-01-01
      相关资源
      最近更新 更多