无描述符的Jersey servlet容器在servlet 3.x容器中作为过滤器运行



有没有一种方法可以在servlet 3.x容器中以javax.servlet.Filter的形式运行Jersey servlet容器(2.x(描述符?我需要在服务的同时提供静态资源,因此需要使用jersey.config.servlet.filter.forwardOn404jersey.config.servlet.filter.staticContentRegex,它们只有在根据Javadoc 作为过滤器运行时才能工作

该属性仅在Jersey servlet容器配置为作为javax.servlet.Filter运行时适用,否则该属性将被忽略。

我想完全摆脱web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
            http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <display-name>My-Webservice</display-name>
    <filter>
        <filter-name>Jersey Filter</filter-name>
        <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.foo.webservices.MyApplication</param-value>
        </init-param>
    </filter>
</web-app>

并在我的自定义Application类中拥有所有内容

@ApplicationPath(value = "/")
public class MyApplication extends ResourceConfig
{    
    public MyApplication()
    {
        packages("com.foo.webservices.services");
        property(ServletProperties.FILTER_FORWARD_ON_404, true);
    }
}

官方文件(https://jersey.java.net/documentation/latest/deployment.html#deployment.servlet.3)不幸的是,没有说明任何关于过滤器的内容。

这是可能的,但不会像设置一些配置属性那样容易。如果你稍微了解一下它的实际工作原理,这会有所帮助。在Servlet 3.x中,引入了一个ServletContainerInitializer,我们可以实现它来动态加载Servlet(这里将进一步讨论(。Jersey有一个它使用的实现。但是它遵循JAX-RS,后者规定应用程序应该作为servlet加载。所以泽西岛对此没有任何办法。

我们可以编写自己的ServletContainerInitializer,也可以直接使用Jersey的。泽西岛有一个我们可以实施的SerletContainerProvider。我们需要自己注册servlet过滤器。实现看起来像这个

@Override
public void preInit(ServletContext context, Set<Class<?>> classes) throws ServletException {
    final Class<? extends Application> applicationCls = getApplicationClass(classes);
    if (applicationCls != null) {
        final ApplicationPath appPath = applicationCls.getAnnotation(ApplicationPath.class);
        if (appPath == null) {
            LOGGER.warning("Application class is not annotated with ApplicationPath");
            return;
        }
        final String mapping = createMappingPath(appPath);
        addFilter(context, applicationCls, classes, mapping);
        // to stop Jersey servlet initializer from trying to register another servlet
        classes.remove(applicationCls);
    }
}
private static void addFilter(ServletContext context, Class<? extends Application> cls,
                              Set<Class<?>> classes, String mapping) {
    final ResourceConfig resourceConfig = ResourceConfig.forApplicationClass(cls, classes);
    final ServletContainer filter = new ServletContainer(resourceConfig);
    final FilterRegistration.Dynamic registration = context.addFilter(cls.getName(), filter);
    registration.addMappingForUrlPatterns(null, true, mapping);
    registration.setAsyncSupported(true);
}

一旦我们实现了,我们需要创建一个文件

META-INF/services/org.glassfish.jersey.servlet.internal.spi.ServletContainerProvider

它应该位于类路径的根。该文件的内容应该是我们实现的完全限定名称。

你可以在这个GitHub Repo 中看到一个完整的例子