简体   繁体   English

Jersey Api 未通过过滤器发送请求

[英]Jersey Api not sending request through Filter

The ContainerRequestFilter is not being applied when receving request.接收请求时未应用 ContainerRequestFilter。

This is my fist Stackoverflow question so i do not really know what details to add in my question.这是我的第一个 Stackoverflow 问题,所以我真的不知道要在我的问题中添加哪些细节。

Here is my filter这是我的过滤器

package tjo.tjaa;

@Provider
 class Securityfilter implements ContainerRequestFilter {


    private static final String AUTHORIZATION_HEADER="authorization";
    private static final String AUTHORIZATION_HEADER_PREFIX="Basic";
    private static final String SECURITY_URL="security";

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {

        //this is where i want to execute code
        return;
 }

}

And here is my web.xml这是我的 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 -->
<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">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>tjo.tjaa</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

I am assuming there are multiple ways to add filter implementations to the JAX-RS.我假设有多种方法可以将过滤器实现添加到 JAX-RS。 One way to add custom filters is to add them to the Application class implementation.添加自定义过滤器的一种方法是将它们添加到应用程序 class 实现中。 In your case, you would have an implementation of Application class and provide the custom filter under getClasses API:在您的情况下,您将拥有应用程序 class 的实现,并在 getClasses API 下提供自定义过滤器:

public class RestApplication extends Application {

  @Override
  public Set<Class<?>> getClasses() {
    return Sets.newHashSet(
        SecurityFilter.class);
  }
}

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

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