JAX-RS 过滤器未被调用
JAX-RS filter not getting called
我在文件 CheckSessionFilter.java 中定义了一个过滤器,如下所示:
@Provider
@SessionChecker
public class CheckSessionFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws WebApplicationException {
....
}
在另一个文件中,SessionChecker.java,我有:
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(value = RetentionPolicy.RUNTIME)
@NameBinding
public @interface SessionChecker {}
我的 Web 服务在 Services.java 中定义为:
@Path("Services")
public class Services {
@POST
@SessionChecker
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("LogIn")
public Response logIn(@Context HttpServletRequest request, Parameters requestParameters) {
....
}
}
我在 WebLogic 12.2.1 上部署了我的 Web 服务。 Web 服务工作正常。但是过滤器根本没有被调用。有什么问题吗?
我需要在 web.xml 中添加任何内容吗?
提前致谢。
编辑为包括 web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>XXXXXXXX</display-name>
<servlet>
<servlet-name>JAX-RS Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>rest.application.config.ApplicationConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
已更新web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>XXXXXXXX</display-name>
<servlet>
<servlet-name>JAX-RS Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>rest.application.config.ApplicationConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
编辑以添加应用程序配置:
package rest.application.config;
import java.util.Set;
import javax.ws.rs.core.Application;
import javax.ws.rs.ApplicationPath;
@ApplicationPath("resources")
public class ApplicationConfig extends Application {
public Set<Class<?>> getClasses() {
return getRestClasses();
}
//Auto-generated from RESTful web service wizard
private Set<Class<?>> getRestClasses() {
Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
resources.add(Services.class);
return resources;
}
}
感谢peeskillet,我找到了解决方案,就是在ApplicationConfig class中添加过滤器,如下代码所示。我还需要使用 org.glassfish.jersey.servlet.ServletContainer 作为 web.xml 中的 servlet 容器,用于 Jersey 2.x.
package rest.application.config;
import java.util.Set;
import javax.ws.rs.core.Application;
import javax.ws.rs.ApplicationPath;
@ApplicationPath("resources")
public class ApplicationConfig extends Application {
public Set<Class<?>> getClasses() {
return getRestClasses();
}
//Auto-generated from RESTful web service wizard
private Set<Class<?>> getRestClasses() {
Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
resources.add(Services.class);
resources.add(CheckSessionFilter.class);
return resources;
}
}
我在文件 CheckSessionFilter.java 中定义了一个过滤器,如下所示:
@Provider
@SessionChecker
public class CheckSessionFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws WebApplicationException {
....
}
在另一个文件中,SessionChecker.java,我有:
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(value = RetentionPolicy.RUNTIME)
@NameBinding
public @interface SessionChecker {}
我的 Web 服务在 Services.java 中定义为:
@Path("Services")
public class Services {
@POST
@SessionChecker
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("LogIn")
public Response logIn(@Context HttpServletRequest request, Parameters requestParameters) {
....
}
}
我在 WebLogic 12.2.1 上部署了我的 Web 服务。 Web 服务工作正常。但是过滤器根本没有被调用。有什么问题吗?
我需要在 web.xml 中添加任何内容吗?
提前致谢。
编辑为包括 web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>XXXXXXXX</display-name>
<servlet>
<servlet-name>JAX-RS Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>rest.application.config.ApplicationConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
已更新web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>XXXXXXXX</display-name>
<servlet>
<servlet-name>JAX-RS Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>rest.application.config.ApplicationConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
编辑以添加应用程序配置:
package rest.application.config;
import java.util.Set;
import javax.ws.rs.core.Application;
import javax.ws.rs.ApplicationPath;
@ApplicationPath("resources")
public class ApplicationConfig extends Application {
public Set<Class<?>> getClasses() {
return getRestClasses();
}
//Auto-generated from RESTful web service wizard
private Set<Class<?>> getRestClasses() {
Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
resources.add(Services.class);
return resources;
}
}
感谢peeskillet,我找到了解决方案,就是在ApplicationConfig class中添加过滤器,如下代码所示。我还需要使用 org.glassfish.jersey.servlet.ServletContainer 作为 web.xml 中的 servlet 容器,用于 Jersey 2.x.
package rest.application.config;
import java.util.Set;
import javax.ws.rs.core.Application;
import javax.ws.rs.ApplicationPath;
@ApplicationPath("resources")
public class ApplicationConfig extends Application {
public Set<Class<?>> getClasses() {
return getRestClasses();
}
//Auto-generated from RESTful web service wizard
private Set<Class<?>> getRestClasses() {
Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
resources.add(Services.class);
resources.add(CheckSessionFilter.class);
return resources;
}
}