Servlet filtering using java EE 6 annotation?

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"; } } 

If the URL is http://foobar.com/api/foo/bar , but the filter method will be called as if it were a servlet filter chain. I know that the approach above does not work, but is there an annotated approach in this question that would achieve the same as if the "Filter" were configured from the web.xml file?

+7
source share
1 answer

JBoss 7 (even JBoss 6 already) supports Java EE 6, which in turn covers Servlet 3.0. Perhaps your web.xml incorrectly declared compatible with Servlet 2.5, because of which @WebFilter does not work at all. Make sure the root declaration of your web.xml been declared appropriate for servlet 3.0 as 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 you showed there are part of JAX-RS, which is another API (RESTful webservice API) built on top of Servlets. To learn more about JAX-RS, a Jersey user guide may be helpful.

See also:

+13
source

All Articles