Search code examples
javafilterjava-ee-7

How filter specifics URL's on my filter that implements ContainerRequestFilter and ContainerResponseFilter?


I am implementing a Filter like below:

@Provider
public class MyFilter implements ContainerRequestFilter, ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
       System.out.print("request");
    }

    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
       System.out.print("response");
    }

}

How do I make the filter work only for URL's that follow a certain pattern (regexp)?


Solution

  • Regex is not allowed in a filter - some wildcards are

    <filter>
        <filter-name>MyFilter</filter-name>
        <filter-class>...</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name> MyFilter</filter-name>
        <url-pattern>/Employee/*</url-pattern>
    </filter-mapping>
    

    You can narrow down the URLs using a Filter then inside your filter Java code, you can use RegEx for conditional processing