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?
JBoss 7 (even JBoss 6 already) supports Java EE 6 which in turn covers Servlet 3.0. Perhaps your
web.xmlis incorrectly been declared conform Servlet 2.5 which caused the@WebFilternot to work at all. Ensure that the root declaration of yourweb.xmlis been declared conform Servlet 3.0 like follows:Then you can just use
@WebFilter: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: