Jax-Rs过滤器无法正常工作

时间:2015-08-19 11:01:10

标签: java java-ee filter jersey jax-rs

我正在处理restful服务,我需要使用身份验证过滤器验证请求。当我调用服务时,我的过滤器类方法没有被触发。

我正在泽西2上工作.--

的pom.xml

    <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>

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>    
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.bundles</groupId>
        <artifactId>jaxrs-ri</artifactId>
    </dependency>

    <properties>
        <jersey.version>2.13</jersey.version>
    </properties>

的web.xml

  <servlet>  
    <servlet-name>Sample Service</servlet-name>  
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>  
      <param-name>jersey.config.server.provider.packages</param-name>  
      <param-value>com.sample.filter,com.sample.api</param-value>  
    </init-param>

    <init-param>
              <param-name>javax.ws.rs.container.ContainerRequestFilters</param-name>
              <param-value>com.sample.filter.RestAuthenticationFilter</param-value>
    </init-param>

    <init-param>  
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>  
        <param-value>true</param-value>  
    </init-param>   
    <load-on-startup>1</load-on-startup>  
  </servlet>

RestAutheticationFilter类

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;

@Provider
public class RestAuthenticationFilter implements ContainerRequestFilter {
    @Override
    public void filter(ContainerRequestContext requestContext)
            throws IOException {
        // TODO Auto-generated method stub
        System.out.println("------------- Entering RestAuthenticationFilter -------------");
    }
}

我无法通过上面的方式触发filter()方法。我也尝试了注释,如下所示。

接口

@NameBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
public @interface Authenticated {}

RestAuthenticationFilter类

@Provider
@Authenticated
public class RestAuthenticationFilter implements ContainerRequestFilter {
    @Override
    public void filter(ContainerRequestContext requestContext)
            throws IOException {
        // TODO Auto-generated method stub
        System.out.println("------------- Entering RestAuthenticationFilter -------------");
    }          
}

网络方法

   @GET
    @Authenticated
    @Path("{id}")
    @ApiOperation(value="Retrievs a single msg")
    @Produces(value = MediaType.APPLICATION_JSON)
    public Message getMsg(

我花了很多时间,却无法弄清楚出了什么问题。感谢是否有人能够找到未触发filter()方法的原因。

0 个答案:

没有答案