0
我想與Jersy abd Tomcat 7一起工作的CDI攔截器。但它從來沒有工作。有人可以幫幫我嗎。CDI與澤西島和Tomcat7 - 攔截器永遠不會被調用
我有點嘗試由澤西提供的例子進行一些小的修改。
這是我的代碼。有趣的是,我可以在輸出中看到消息「Injected .....」,意味着@PostConstruct註釋正在工作。在WEB-INF
<beans>
<interceptors>
<class>org.glassfish.jersey.examples.cdi.resources.LoggedInterceptor</class>
</interceptors>
<beans/>
Logged.java
的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
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);
}
}
您是否對焊接servlet有依賴性? PostConstruct可以由JAX-RS運行時以及CDI運行時調用。 – 2014-12-05 10:33:08
可能是http://stackoverflow.com/questions/27269482/java-cdi-interceptor-not-working-web-application-with-weld,但是這個問題沒有被正確回答。 – 2014-12-05 10:34:22