gpt4 book ai didi

jakarta-ee - 参数拦截器在 Java EE 中不起作用

转载 作者:行者123 更新时间:2023-12-01 16:10:29 27 4
gpt4 key购买 nike

在 Java EE 中,当方法参数具有特定注释时,我尝试调用拦截器。这是我的代码:

注释的代码

@Retention(RUNTIME)
@Target(PARAMETER)
public @interface Token{
//NOP
}

拦截器代码

@Interceptor
public class TokenInterceptor{

@AroundInvoke
public Object checkInvocation(InvocationContext ctx) throws Exception {
//Actual Code that detects the presence of the annotation
}

}

带注释参数的方法

public void processOrders(@Token List<Order> token) {}

beans.xml

<interceptors>
<class>com.project.security.TokenInterceptor</class>
</interceptors>

当我尝试部署 JBoss 服务器时,出现以下错误。

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-000069 An interceptor must have at least one binding, but com.project.security.TokenInterceptor has none at 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:229) at org.jboss.weld.bootstrap.BeanDeployer.createBeans(BeanDeployer.java:149) at org.jboss.weld.bootstrap.BeanDeployment.createBeans(BeanDeployment.java:204) at org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java: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

我想知道我缺少什么才能让它发挥作用吗?

最佳答案

您需要为每个这样的拦截器进行绑定(bind)

@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface Logged {
}

拦截器本身需要用您的绑定(bind)进行注释

@Interceptor
@Logged
public void TokenInterceptor {

@AroundInvoke
public Object checkInvocation(InvocationContext ctx) throws Exception {
//Actual Code that detects the presence of the annotation
}
}

现在您可以使用 @Logged 注释将拦截器绑定(bind)到任何方法或类

@Logged
public void processOrders(List<Order> token) {}

请参阅官方 Java EE 教程 reference .

关于jakarta-ee - 参数拦截器在 Java EE 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25158980/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com