Parameter interceptor not working in Java EE
In Java EE, I am trying to call an interceptor when a method parameter has a specific annotation. This is the code I have:
Annotation code
@Retention(RUNTIME)
@Target(PARAMETER)
public @interface Token{
//NOP
}
Interceptor code
@Interceptor
public class TokenInterceptor{
@AroundInvoke
public Object checkInvocation(InvocationContext ctx) throws Exception {
//Actual Code that detects the presence of the annotation
}
}
Method with annotated parameter
public void processOrders(@Token List<Order> token) {}
beans.xml
<interceptors>
<class>com.project.security.TokenInterceptor</class>
</interceptors>
When I try to deploy my JBoss server, below error occurs.
Throws: org.jboss.weld.exceptions.DeploymentException: WELD-000069 The interceptor must have at least one binding, but com.project.security.TokenInterceptor does not have on org.jboss.weld.bean.InterceptorImpl. (InterceptorImpl.java:72) at org.jboss.weld.bean.InterceptorImpl.of (InterceptorImpl.java:59) at org.jboss.weld.bootstrap.AbstractBeanDeployer.createInterceptor (AbstractBeanDeployer.java.j229) at. weld.bootstrap.BeanDeployer.createBeans (BeanDeployer.java:149) at org.jboss.weld.bootstrap.BeanDeployment.createBeans (BeanDeployment.java:204) at org.jboss.weld.bootbotstrap.Weldanstrap 349) at org.jboss.as.weld.WeldStartService.start (WeldStartService.java:63) at org.jboss.msc.service.ServiceControllerImpl $ StartTask.startService (ServiceControllerImpl.java:1811) [jboss-msc-1.0.4 ...GA-redhat-1.jar: 1.0.4.GA-redhat-1] at org.jboss.msc.service.ServiceControllerImpl $ StartTask.run (ServiceControllerImpl.java:1746) [jboss-msc-1.0.4.GA- redhat-1.jar: 1.0.4.GA-redhat-1] ... 3 more
Any ideas on what I am missing to get it to work?
You need to have a binding for each interceptor like this
@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface Logged {
}
Than the interceptor itself should be annotated with your binding
@Interceptor
@Logged
public void TokenInterceptor {
@AroundInvoke
public Object checkInvocation(InvocationContext ctx) throws Exception {
//Actual Code that detects the presence of the annotation
}
}
Now you can bind your interceptor to any method or class using the annotation @Logged
@Logged
public void processOrders(List<Order> token) {}
See the official Java EE tutorial for a link .