简体   繁体   English

使用Java EE 6注释进行Servlet过滤?

[英]Servlet Filtering using java EE 6 annotation?

Is it possible to simulate a servlet filter chain using @ApplicationPath and @Path annotations in EE 6? 是否可以使用EE 6中的@ApplicationPath和@Path注释来模拟servlet过滤器链?

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. URL的位置是http://foobar.com/api/foo/bar,但“filter”方法将被调用,就像它是一个servlet过滤器链一样。 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? 我知道上面的方法不会起作用,但是在这种方法中是否有一个注释方法可以实现与从web.xml文件配置“过滤器”相同的方法?

JBoss 7 (even JBoss 6 already) supports Java EE 6 which in turn covers Servlet 3.0. JBoss 7(甚至已经是JBoss 6)支持Java EE 6,后者又涵盖了Servlet 3.0。 Perhaps your web.xml is incorrectly been declared conform Servlet 2.5 which caused the @WebFilter not to work at all. 也许您的web.xml被错误地声明为符合Servlet 2.5,导致@WebFilter根本无法工作。 Ensure that the root declaration of your web.xml is been declared conform Servlet 3.0 like follows: 确保web.xml的根声明已声明符合Servlet 3.0,如下所示:

<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

@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. 你在那里展示的例子是JAX-RS的一部分,它是在Servlets之上构建的另一个API(RESTful webservice API)。 To learn more about JAX-RS, the Jersey user guide may be useful. 要了解有关JAX-RS的更多信息, Jersey用户指南可能很有用。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM