ContainerRequestFilter ContainerResponseFilter 没有被调用 [英] ContainerRequestFilter ContainerResponseFilter doesn't get called

查看:34
本文介绍了ContainerRequestFilter ContainerResponseFilter 没有被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过创建一个小型 RESTful 服务来学习球衣.我想出于特定原因使用过滤器(比如我想将 ContainerResponseFilter 用于 CORS 标头以允许跨域请求).但是,我只是无法让这些过滤器拦截我的电话.我已经看过所有关于这个问题的帖子,其中大多数都说要在注释提供者或 web.xml 中注册.我尝试在 web.xml 中注册文件并为容器提供 @Provider 注释

I am trying to learn jersey by creating a small RESTful service. I want to use the Filters for specific reasons(Like I want to use the ContainerResponseFilter for CORS headers to allow cross domain requests). However, I am just not able to get these filters intercept my call. I have seen all the posts for this problem and most of them say to register with annotation provider or in web.xml. I have tried registering the files in web.xml as well as giving a @Provider annotation for the container

这是我的 web.xml

Here is my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container, 
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html#d4e194 -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/spring/config/BeanLocations.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.rest.example</param-value>
        </init-param>
        <init-param>  
            <param-name>jersey.config.server.provider.packages</param-name>  
            <param-value>com.rest.example.cors</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
            <param-value>com.rest.example.CORSFilter</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
            <param-value>com.rest.example.RequestFilter</param-value>
        </init-param>

    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webresources/*</url-pattern>
    </servlet-mapping>

</web-app>

这是我的过滤器:

package com.rest.example.cors;

import javax.ws.rs.ext.Provider;

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;

@Provider
public class CORSFilter implements ContainerResponseFilter {

    public ContainerResponse filter(ContainerRequest creq,
            ContainerResponse cresp) {

             cresp.getHttpHeaders().putSingle("Access-Control-Allow-Origin", "*");
             cresp.getHttpHeaders().putSingle("Access-Control-Allow-Credentials", "true");
             cresp.getHttpHeaders().putSingle("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS, HEAD");
             cresp.getHttpHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");

            return cresp;
    }
}


package com.rest.example.cors;

import javax.ws.rs.ext.Provider;

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;

@Provider
public class RequestFilter implements ContainerRequestFilter {

    public ContainerRequest filter(ContainerRequest request) {
        System.out.println("request filter");
        return request;
    }
}

链接到我的 github 项目.

Link to my github project.

推荐答案

我添加了一个 Jersey Application 类,并在该类中注册了过滤器,解决了我的问题.还将我的球衣版本从 1.x 升级到 2.x

I added a Jersey Application class and registered the filter in the class, which solved my problem. Also upgraded my jersey version to 2.x from 1.x

public class MyApplication extends ResourceConfig {

    /**
     * Register JAX-RS application components.
     */
    public MyApplication () {
        register(RequestContextFilter.class);
        register(JacksonFeature.class);
        register(CustomerResource.class);
        register(Initializer.class);
        register(JerseyResource.class);
        register(SpringSingletonResource.class);
        register(SpringRequestResource.class);
        register(CustomExceptionMapper.class);
    }
}

这篇关于ContainerRequestFilter ContainerResponseFilter 没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆