ContainerRequestFilter 不会在 JavaEE jersey 项目中 运行
ContainerRequestFilter wont run in JavaEE jersey project
我是 JavaEE 的新手,在 Jersey 中将自定义 ContainerRequestFilter 设置为 运行 时遇到了一些问题。我更多地阅读了球衣文档并直接从'jersey-quickstart-webapp'创建了一个新的干净项目,添加了如下所示的过滤器但是运气不好(也添加了一个空 beans.xml)。
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
@Provider
@PreMatching
public class MyFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).entity("User cannot access the resource.").build());
}
}
不确定 Prematching 和 Provider 是否互补,所以我分别使用了两者,但没有用(MyReasource 只是没有过滤器)。尝试在 MyFilter 中抛出异常,但也没有 运行。
所以我搜索了 Whosebug 并找到了“http://blog.dejavu.sk/2013/11/19/registering-resources-and-providers-in-jersey-2/”,它指出您实际上需要在 Application 或 ResourceConfig class 中实施注册。我试过了(没用),但我现在至少收到了资源 class 的警告,'没有找到资源 class [=40 的资源方法=]'
我的应用程序 class 现在看起来如下所示(尝试扫描包但没有什么不同。没有手动过滤器注册我也没有收到警告)。
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import javax.ws.rs.ApplicationPath;
@ApplicationPath("resources")
public class RestApplication extends ResourceConfig {
public RestApplication() {
//packages("a.b");
register(MyFilter.class);
register(MyResource.class);
property(ServerProperties.TRACING, "ALL");
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>a.b</groupId>
<artifactId>server</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>server</name>
<build>
<finalName>server</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
</dependencies>
<properties>
<jersey.version>2.22.1</jersey.version>
<failOnMissingWebXml>false</failOnMissingWebXml>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
所有文件都位于 'a.b'(即我的包)根目录中。关于如何实际获得过滤器的任何想法 运行ning,我将非常感激 ;)。我想让它工作应该不会这么难,所以我想我在这里遗漏了什么?
让我向您介绍一下发生了什么。当你第一次创建 jersey-quickstart-webapp
原型时,它给了你这个
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>a.b</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
这个 init-param jersey.config.server.provider.packages
的作用是告诉 Jersey 要扫描哪些包以获取注释为 @Path
的资源 类 和注释为 类 的提供程序@Provider
.
所以从那时起,您需要做的就是将 @Provider
添加到过滤器中,它就会起作用。
但后来您决定清除 web.xml 并使用 ResourceConfig
和 @ApplicationPath
。为此,Jersey 利用了 Servlet 3.0 可插拔机制,如前所述 。为了让它工作,我们需要确保我们有带有 JerseyServletContainerInitializer
的罐子。这就是我们需要查看 pom.xml 文件
的地方
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
评论告诉你,如果你不需要 Servlet 2.5 支持,你应该使用 jersey-container-servlet
而不是 jersey-container-servlet-core
。不知道,可能是表达不好。也许它应该说如果你想要 Servlet 3.x 支持,改变它。但无论如何,jersey-container-servlet
有我们需要的 JerseyContainerServletInitializer
。所以如果你想去 web.xml-less,那么只需切换依赖项。
我是 JavaEE 的新手,在 Jersey 中将自定义 ContainerRequestFilter 设置为 运行 时遇到了一些问题。我更多地阅读了球衣文档并直接从'jersey-quickstart-webapp'创建了一个新的干净项目,添加了如下所示的过滤器但是运气不好(也添加了一个空 beans.xml)。
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
@Provider
@PreMatching
public class MyFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).entity("User cannot access the resource.").build());
}
}
不确定 Prematching 和 Provider 是否互补,所以我分别使用了两者,但没有用(MyReasource 只是没有过滤器)。尝试在 MyFilter 中抛出异常,但也没有 运行。
所以我搜索了 Whosebug 并找到了“http://blog.dejavu.sk/2013/11/19/registering-resources-and-providers-in-jersey-2/”,它指出您实际上需要在 Application 或 ResourceConfig class 中实施注册。我试过了(没用),但我现在至少收到了资源 class 的警告,'没有找到资源 class [=40 的资源方法=]'
我的应用程序 class 现在看起来如下所示(尝试扫描包但没有什么不同。没有手动过滤器注册我也没有收到警告)。
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import javax.ws.rs.ApplicationPath;
@ApplicationPath("resources")
public class RestApplication extends ResourceConfig {
public RestApplication() {
//packages("a.b");
register(MyFilter.class);
register(MyResource.class);
property(ServerProperties.TRACING, "ALL");
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>a.b</groupId>
<artifactId>server</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>server</name>
<build>
<finalName>server</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
</dependencies>
<properties>
<jersey.version>2.22.1</jersey.version>
<failOnMissingWebXml>false</failOnMissingWebXml>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
所有文件都位于 'a.b'(即我的包)根目录中。关于如何实际获得过滤器的任何想法 运行ning,我将非常感激 ;)。我想让它工作应该不会这么难,所以我想我在这里遗漏了什么?
让我向您介绍一下发生了什么。当你第一次创建 jersey-quickstart-webapp
原型时,它给了你这个
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>a.b</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
这个 init-param jersey.config.server.provider.packages
的作用是告诉 Jersey 要扫描哪些包以获取注释为 @Path
的资源 类 和注释为 类 的提供程序@Provider
.
所以从那时起,您需要做的就是将 @Provider
添加到过滤器中,它就会起作用。
但后来您决定清除 web.xml 并使用 ResourceConfig
和 @ApplicationPath
。为此,Jersey 利用了 Servlet 3.0 可插拔机制,如前所述 JerseyServletContainerInitializer
的罐子。这就是我们需要查看 pom.xml 文件
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
评论告诉你,如果你不需要 Servlet 2.5 支持,你应该使用 jersey-container-servlet
而不是 jersey-container-servlet-core
。不知道,可能是表达不好。也许它应该说如果你想要 Servlet 3.x 支持,改变它。但无论如何,jersey-container-servlet
有我们需要的 JerseyContainerServletInitializer
。所以如果你想去 web.xml-less,那么只需切换依赖项。