7

Is it possible to simulate a servlet filter chain using @ApplicationPath and @Path annotations in EE 6?

Example:

@ApplicationPath("/api")
class Filter extends Application { 
    @Path("/*")
    public void filter() {
        log.info("Request to API");
    }
}

...

@Path("/foo")
class Foo {
    @GET
    @Path("/bar")
    @Produces("text/plain")
    public String bar() {
        return "Hello World";
    }
}

Where the URL would be http://foobar.com/api/foo/bar but the "filter" method would be invoked as if it were a servlet filter chain. I know the approach above wont work but is there an annotated approach in this ilk that would achieve the same as if the "Filter" was configured from web.xml file?

travega
  • 8,284
  • 16
  • 63
  • 91

1 Answers1

13

JBoss 7 (even JBoss 6 already) supports Java EE 6 which in turn covers Servlet 3.0. Perhaps your web.xml is incorrectly been declared conform Servlet 2.5 which caused the @WebFilter not to work at all. Ensure that the root declaration of your web.xml is been declared conform Servlet 3.0 like follows:

<web-app 
    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_3_0.xsd"
    version="3.0">

Then you can just use @WebFilter:

@WebFilter("/api/*")
public class FooFilter implements Filter {

    // ...

}

The examples which you've shown there are by the way part of JAX-RS, which is another API (a RESTful webservice API) built on top of Servlets. To learn more about JAX-RS, the Jersey user guide may be useful.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Will this allow me to configure the filters without a web.xml file or can I use annotation only? – travega Nov 16 '11 at 04:35
  • You don't need to specify them by `` and `` in `web.xml`. But if you already didn't have a `web.xml`, then that's also perfectly fine. The inability to use `@WebFilter` and stating that you're using Servlet 2.5 as you implied yourself however indicates that you *already* have a `web.xml` which in turn is incorrectly been declared conform Servlet 2.5. Or didn't you actually tried anything yourself at all? – BalusC Nov 16 '11 at 04:40
  • Thanks for clarifying what I said!! Yes have tried a fair bit myself - Im porting software from JBoss 6 to Jboss 7. Now back to the question: "...is there an annotated approach in this ilk that would achieve the same as if the "Filter" was configured from web.xml file?". Quite clearly I am looking for a solution that does not require a web.xml file. Thx – travega Nov 16 '11 at 19:54
  • I have already answered that: use `@WebFilter`. Read the docs for all options of the annotation: http://download.oracle.com/javaee/6/api/javax/servlet/annotation/WebFilter.html The only which you cannot control is the ordering of the filters. For that you still really need `web.xml`, see also http://stackoverflow.com/questions/6560969/how-to-define-servlet-filter-order-of-execution-using-annotations/6561816#6561816 – BalusC Nov 16 '11 at 19:55