使用Java EE的Servlet过滤6注解? [英] Servlet Filtering using java EE 6 annotation?

查看:232
本文介绍了使用Java EE的Servlet过滤6注解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用模拟和@ApplicationPath注释@Path一个Servlet过滤器链EE 6?

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

例如:

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

当URL会 http://foobar.com/api/foo/bar 但过滤器就好像它是一个Servlet过滤器链的方法将被调用。我知道上面不会工作的方法,但有这个之流的注释方法,将取得相同的,如果过滤器从web.xml文件中配置?

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?

推荐答案

7的JBoss(甚至6的JBoss已经)支持Java EE 6反过来涵盖的Servlet 3.0。也许你的的web.xml 错误地被宣布为符合Servlet 2.5的这引起了 @WebFilter 不是在所有的工作。确保是被宣布为你的的web.xml的根申报符合的Servlet 3.0一样如下:

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

然后,你可以使用<一个href=\"http://download.oracle.com/javaee/6/api/javax/servlet/annotation/WebFilter.html\"><$c$c>@WebFilter:

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

    // ...

}

您已经证明那里的例子是通过JAX-RS的方式的一部分,这是一个建立在servlet之上的另一个API(一个RESTful Web服务API)。要了解更多关于JAX-RS,href=\"http://jersey.java.net/nonav/documentation/latest/user-guide.html\">新泽西用户指南可能是有用的

  • Our Servlet-Filters wiki page

这篇关于使用Java EE的Servlet过滤6注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆