在没有web.xml的泽西岛中使用ContainerRequestFilter [英] Use ContainerRequestFilter in Jersey without web.xml

查看:96
本文介绍了在没有web.xml的泽西岛中使用ContainerRequestFilter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试拦截Glassfish内部运行的Jersey中的请求.

I am trying to intercept requests in Jersey running inside Glassfish.

我创建了ContainerRequestFilter

package mycustom.api.rest.security;

@Provider
public class SecurityProvider implements ContainerRequestFilter {
  @Override
  public ContainerRequest filter(ContainerRequest request) {
    return request;
  }
}

我的应用是使用PackagesResourceConfig的子类启动的.

My app is started using a subclass of PackagesResourceConfig.

Glassfish启动时,泽西岛找到我的提供者:

When Glassfish starts, Jerseys find my provider:

INFO: Provider classes found:
  class mycustom.rest.security.SecurityProvider

但是它永远不会碰到filter方法.我想念什么?

But it never hits that filter method. What am I missing??

其他一切似乎都正常.我添加了几个ContextResolver提供程序来进行JSON映射,它们可以正常工作.请求可以很好地利用我的资源,它永远不会通过过滤器.

Everything else seems to be working fine. I added a couple of ContextResolver providers to do JSON mapping and they work fine. Requests hit my resources fine, it just never goes through the filter.

推荐答案

我不认为容器筛选器是作为提供程序加载的.我认为您必须设置响应过滤器属性.奇怪的是PackagesResourceConfig没有setProperty(),但是您可以重载getProperty()getProperties():

I don't think container filters are loaded in as providers. I think you have to set the response filters property. Strangely PackagesResourceConfig doesn't have a setProperty() but you could overload getProperty() and getProperties():

public Object getProperty(String propertyName) {
  if(propertyName.equals(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS)) {
    return new String[] {"mycustom.rest.security.SecurityProvider"};
  } else {
    return super.getProperty(propertyName);
  }
}

public Map<String,Object> getProperties() {
  propName = ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS;
  Map<String,Object> result = super.getProperties();
  result.put(propName,getProperty(propName));
  return result;
}

实际上,仔细阅读javadocs似乎是首选方法:

Actually, reading the javadocs more closely, it appears the preferred method is:

myConfig.getProperties().put(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS,
                              new String [] {"mycustom.rest.security.SecurityProvider"});

这篇关于在没有web.xml的泽西岛中使用ContainerRequestFilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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