简体   繁体   English

未调用ContainerRequestFilter方法

[英]ContainerRequestFilter method not being called

I've just started learning how to code REST web services, and I've been stuck with this for several days now. 我刚刚开始学习如何编写REST Web服务代码,而现在我已经坚持了几天。 I'm coding an example application with header-based filtering, using Jersey 2 and deployed on Tomee-plus 1.7.2. 我正在使用Jersey 2并部署在Tomee-plus 1.7.2上,使用基于标头的过滤来编码示例应用程序。 No matter what I try, the ContainerRequestFilter's filter method is never called. 不管我尝试什么,都不会调用ContainerRequestFilter的filter方法。

// TestRequestFilter.java
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.ext.Provider;

@Provider
@PreMatching
public class TestRequestFilter implements ContainerRequestFilter
{
    private final static Logger log = Logger.getLogger(DemoRESTRequestFilter.class.getName());

    @Override
    public void filter(ContainerRequestContext requestCtx) throws IOException
    {
        System.out.println("FILTER-REQUEST");
    }
}

My web.xml file is empty save for the required headers. 我的web.xml文件为空,但保留了必需的标头。 The behavior right now is: the filter is recognised as @Provider and instantiated as normal, the test web service I have (just a GET returning an empty Response) can be called normally, but the filter method is never called. 现在的行为是:筛选器被识别为@Provider并被正常实例化,可以正常调用我拥有的测试Web服务(只是GET返回一个空的Response),但是从不调用filter方法。

Things I've tried and their effects: 我尝试过的事情及其影响:

  • Declare the filter in a class extending Application: Error on deployment. 在扩展“应用程序:部署时出错”的类中声明过滤器。
  • Register the filter in a class extending ResourceConfig: Filter is instantiated twice, but filter method is still not called. 在扩展ResourceConfig的类中注册过滤器:过滤器实例化两次,但仍未调用过滤器方法。
  • Use the classes from the com.sun.jersey.spi.container package: No effect. 使用com.sun.jersey.spi.container包中的类:无效。
  • Add an authentication annotation (@RolesAllowed, @PermitAll,...) to the WS method: No effect. 将认证注释(@ RolesAllowed,@ PermitAll等)添加到WS方法中:无效。
  • Add disabled=true to cfx-rs.properties in server configuration: Deployed service cannot be found at usual URL. 在服务器配置的cfx-rs.properties中添加disabled = true :在常规URL上找不到部署的服务。
  • Add this to web.xml : No effect. 将此添加到web.xml中 :无效。

 <servlet> <servlet-name>CongressAppWS</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>org.glassfish.jersey.spi.container.ContainerRequestFilters</param-name> <param-value>com.s4w.congressapp.auth.DemoRESTRequestFilter;com.s4w.congressapp.auth.DemoRESTResponseFilter</param-value> </init-param> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.s4w.congressapp.auth;com.s4w.congressapp.resources</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> 

  • Using com.sun.jersey.spi.container prefix instead of org.glassfish.jersey.spi.container in previous code: No effect. 在前面的代码中使用com.sun.jersey.spi.container前缀代替org.glassfish.jersey.spi.container :无效。

I'm honestly running out of options here. 老实说,我这里的选项已用完。 Every time I try something new, either there is no effect or everything stops working. 每当我尝试一些新的东西时,要么没有效果,要么一切都停止了。 Any help? 有什么帮助吗?

I found an alternative to ContainerRequestFilter that actually works! 我找到了一个可以代替ContainerRequestFilter的替代方法,该替代方法确实有效! It's javax.servlet.Filter . 它是javax.servlet.Filter Extending from this class, all I had to do was annotate it with the following code and the filtering mechanism works like a charm. 从该类扩展之后,我要做的就是用以下代码对其进行注释,并且过滤机制像一个魅力一样起作用。

@WebFilter(filterName = "AuthenticationFilter", urlPatterns = { "/*" })

The reason for this is that ContainerRequestFilter is part JAX-RS 2.0, but TomEE 1.7.X comes with JAX-RS 1.1. 原因是ContainerRequestFilter是JAX-RS 2.0的一部分,但是TomEE 1.7.X随JAX-RS 1.1一起提供。 You'll either have to upgrade TomEE to 7.0.0+ or use a different Server (eg Glassfish). 您必须将TomEE升级到7.0.0+或使用其他服务器(例如Glassfish)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM