使用Jersey和Tomcat7的CDI - 永远不会调用拦截器

时间:2014-12-04 20:24:49

标签: java jersey tomcat7 cdi

我试图用Jersy abd Tomcat 7工作CDI拦截器但它从未起作用。有人可以帮助我。

我有点尝试泽西岛提供的示例,只做了一些小修改。

这是我的代码。有趣的是,我可以看到消息" Injected ....."在输出中,表示@PostConstruct注释正在工作。

的pom.xml

    <dependencies>
            <dependency>
                <groupId>javax.ws.rs</groupId>
                <artifactId>javax.ws.rs-api</artifactId>
                <version>2.0</version>
            </dependency>
            <dependency>
                <groupId>javax.annotation</groupId>
                <artifactId>javax.annotation-api</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency>
                <groupId>javax.enterprise</groupId>
                <artifactId>cdi-api</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency> <!-- this is to avoid Jersey jars to be bundled with the WAR -->
               <groupId>org.glassfish.jersey.containers</groupId>
               <artifactId>jersey-container-servlet-core</artifactId>
               <version>2.13</version>
            <!--    <scope>provided</scope> -->
            </dependency>
            <dependency>
        <groupId>org.glassfish.jersey.containers.glassfish</groupId>
        <artifactId>jersey-gf-cdi</artifactId>
        <version>2.13</version>
    </dependency>
        </dependencies>

WEB-INF中的beans.xml

<beans>
    <interceptors>
        <class>org.glassfish.jersey.examples.cdi.resources.LoggedInterceptor</class>
     </interceptors>


<beans/>

Logged.java

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.interceptor.InterceptorBinding;
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Logged {

}

LoggedInterceptor.java

import java.io.Serializable;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;


@Interceptor
@Logged

public class LoggedInterceptor implements Serializable {

    public LoggedInterceptor() {
        System.out.println("Invoked....");
    }

    @AroundInvoke
    public Object logMethodEntry(InvocationContext invocationContext)
            throws Exception {
        System.out.println("Entering method: "
                + invocationContext.getMethod().getName() + " in class "
                + invocationContext.getMethod().getDeclaringClass().getName());

        return invocationContext.proceed();
    }
}

LoggedInterceptorTest.java

public class LoggedInterceptorTest {


    @Logged
    public void testLoggedInterceptor() {
        System.out.println("Called...");
    }

    public static void main(String[] args) {
        LoggedInterceptorTest l = new LoggedInterceptorTest();
        l.testLoggedInterceptor();
    }
}

服务 的 EchoParamFieldResource.java

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;


@ManagedBean
@Path("echofield/{b}")
public class EchoParamFieldResource {

    @PathParam("b") String bInjected;

    String b;

    /**
     * Ensure we got path parameter value injected.
     */
    @PostConstruct
    @SuppressWarnings("unused")
    private void postConstruct() {
        if (bInjected == null) {
            throw new IllegalStateException("Field b has not been injected!");
        }
        b = bInjected;
        System.out.println("Injected.....");
        LoggedInterceptorTest l = new LoggedInterceptorTest();
        l.testLoggedInterceptor();
    }

    /**
     * Return a string containing injected values.
     *
     * @param a value of a query parameter a.
     * @return message containing injected values.
     */
    @GET
    @Produces("text/plain")
    public String get(@QueryParam("a") String a) {
        return String.format("ECHO %s %s", a, b);
    }
}

1 个答案:

答案 0 :(得分:1)

你最有可能只使用Tomcat,而不是TomEE。只有TomEE支持CDI。

@PathParam(&#34; b&#34;)注射由Jersey提供,而不是CDI。拦截器是CDI的一部分。

为了被截获,课程应由CDI管理。所以不要像那样实例化类:

LoggedInterceptorTest l = new LoggedInterceptorTest(); // CDI does not work!

你应该注射它:

@Inject
LoggedInterceptorTest l; 

你可以查看是否l == null,这将是CDI检查。