在web.xml中使用@WebFilter和@WebListener vs <filter>和<listener>(使用Servlet 3.x的Jersy2)

时间:2017-10-11 11:03:23

标签: java java-ee jersey-2.0 java-melody servlet-3.1

我正在尝试在Web应用程序中使用java-melody进行监控。

build.gradle中的依赖

compile group: 'net.bull.javamelody', name: 'javamelody-core', version: '1.69.0'

使用web.xml(Works Fine)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.1"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <filter>
        <filter-name>javamelody</filter-name>
        <filter-class>net.bull.javamelody.MonitoringFilter</filter-class>
        <async-supported>true</async-supported>
    </filter>
    <filter-mapping>
        <filter-name>javamelody</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ASYNC</dispatcher>
    </filter-mapping>
    <listener>
        <listener-class>net.bull.javamelody.SessionListener</listener-class>
    </listener>
</web-app>

应用程序init类

@ApplicationPath(value = "/*")
public class TestJavamelodyApp extends ResourceConfig {

    public TestJavamelodyApp() {
        packages("com.test.resources")
                .register(CustomWebExceptionMapper.class)
                .register(new TestCustomBinder())
    }
}

直到这里工作正常。

但我不想使用web.xml,因为我们可以在Servlet 3.x中执行此操作。所以我确实喜欢下面,这似乎不起作用......

过滤器

@WebFilter(urlPatterns = "/*", asyncSupported = true, dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.ASYNC})
public class JavaMelodyMonitoringFilter extends MonitoringFilter {
}

修改后的应用初始化

@ApplicationPath(value = "/*")
public class TestJavamelodyApp extends ResourceConfig {

    public TestJavamelodyApp() {
        packages("com.test.resources")
                .register(CustomWebExceptionMapper.class)
                .register(new TestCustomBinder())
                .register(MonitoringFilter.class)
                .register(SessionListener.class);
    }
}

我在这里做错了什么? Aren都一样吗?

什么行不通

使用web.xml,我可以访问http://localhost:8080/test-app/monitoring处的资源,所有图表都显示正常。但是,如果没有任何web.xml@WebFilter,则无法访问该资源。

0 个答案:

没有答案
相关问题