未调用JAX-RS过滤器

时间:2016-03-07 05:48:17

标签: web-services rest jax-rs weblogic weblogic12c

我在文件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.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;    
    }
}

1 个答案:

答案 0 :(得分:0)

感谢peeskillet,我找到了解决方案,即在ApplicationConfig类中添加过滤器,如下面的代码所示。我还需要使用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;    
    }
}