Java 使用Jersey和Tomcat7的CDI-从不调用拦截器
我正试图与Jersy abd Tomcat 7合作使用CDI拦截器。但它从未奏效。谁能帮帮我吗 我正在尝试Jersey提供的示例,但做了一些小的修改 这是我的密码。有趣的是,我可以在输出中看到消息“Injected…”,这意味着@PostConstruct注释正在工作 Pom.xmlJava 使用Jersey和Tomcat7的CDI-从不调用拦截器,java,jersey,tomcat7,cdi,Java,Jersey,Tomcat7,Cdi,我正试图与Jersy abd Tomcat 7合作使用CDI拦截器。但它从未奏效。谁能帮帮我吗 我正在尝试Jersey提供的示例,但做了一些小的修改 这是我的密码。有趣的是,我可以在输出中看到消息“Injected…”,这意味着@PostConstruct注释正在工作 Pom.xml <dependencies> <dependency> <groupId>javax.ws.rs</grou
<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>
LoggedInterceptor.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 {
}
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();
}
}
public class LoggedInterceptorTest {
@Logged
public void testLoggedInterceptor() {
System.out.println("Called...");
}
public static void main(String[] args) {
LoggedInterceptorTest l = new LoggedInterceptorTest();
l.testLoggedInterceptor();
}
}
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);
}
}
LoggedInterceptorTest.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 {
}
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();
}
}
public class LoggedInterceptorTest {
@Logged
public void testLoggedInterceptor() {
System.out.println("Called...");
}
public static void main(String[] args) {
LoggedInterceptorTest l = new LoggedInterceptorTest();
l.testLoggedInterceptor();
}
}
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);
}
}
服务
EchoParamFieldResource.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 {
}
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();
}
}
public class LoggedInterceptorTest {
@Logged
public void testLoggedInterceptor() {
System.out.println("Called...");
}
public static void main(String[] args) {
LoggedInterceptorTest l = new LoggedInterceptorTest();
l.testLoggedInterceptor();
}
}
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);
}
}
你很可能只用一只雄猫,而不是一本大部头。只有TomEE支持CDI @PathParam(“b”)注入由Jersey提供,而非CDI。拦截器是CDI的一部分 为了被拦截,类应该由CDI管理。因此,不要像这样实例化类:
LoggedInterceptorTest l = new LoggedInterceptorTest(); // CDI does not work!
你应该:
@Inject
LoggedInterceptorTest l;
您可以检查
l==null
,这将是CDI检查。您是否依赖于weld servlet?JAX-RS运行时和CDI运行时都可以调用PostConstruct。