Reputation: 3207
I'm trying to work CDI interceptors with Jersy abd Tomcat 7. But it never worked. Can some one please help me.
I'm kind of trying the example provided by Jersey with some minor modifications.
Here is my code. Interestingly I can see the message "Injected....." in the output, means @PostConstruct annotation is working.
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>
beans.xml in WEB-INF
<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();
}
}
Service 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);
}
}
Upvotes: 0
Views: 658
Reputation: 1508
You most probably use just a Tomcat, not a TomEE. Only TomEE supports CDI.
The @PathParam("b") injection is provided by Jersey, not CDI. And Interceptors are part of CDI.
And in order to be intercepted, class should be managed by CDI. So instead of instantiating class like that:
LoggedInterceptorTest l = new LoggedInterceptorTest(); // CDI does not work!
You should inject it:
@Inject
LoggedInterceptorTest l;
And you could check if l == null
, and this will be CDI check.
Upvotes: 1