当前位置: 首页 > 知识库问答 >
问题:

如何在自定义CDI拦截器的bean上使用OmniFaces@Param时正确解析NPE

广瑞
2023-03-14

我想在带有自定义CDI拦截器注释的视图控制器上使用OmniFaces@Param:

@Named
@javax.faces.view.ViewScoped
@CustomInterceptor
public class Controller implements Serializable {

  @Param(required = true)
  private String token;
  
  @PostConstruct
  public void initialize() {
    // use this.token
  }
}

自定义CDI拦截器:

@CustomAnnotation
@Interceptor
@Priority(Interceptor.Priority.APPLICATION)
@Dependent
public class CustomInterceptor implements Serializable {

  @PostConstruct
  private Object initialize(InvocationContext invocationContext) throws Exception {
    // some code
    return invocationContext.proceed();
  }

  @AroundInvoke
  private Object intercept(InvocationContext invocationContext) throws Exception {
    // some code
    return invocationContext.proceed();
  }

  @PreDestroy
  private Object shutdown(InvocationContext invocationContext) throws Exception {
    // some code
    return invocationContext.proceed();
  }
}

和自定义注释:

@Inherited
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface CustomAnnotation {}

根据视图的运行时请求,我将获得具有以下堆栈跟踪的NPE:

SEVERE [javax.enterprise.resource.webcontainer.jsf.application] (default task-1) Error Rendering View[view.xhtml]: java.lang.NullPointerException
  at deployment//org.omnifaces.cdi.param.ParamProducer.doBeanValidation(ParamProducer.java:455)
  at deployment//org.omnifaces.cdi.param.ParamProducer.validateBean(ParamProducer.java:359)
  at deployment//org.omnifaces.cdi.param.ParamProducer.invokeValidators(ParamProducer.java:338)
  at deployment//org.omnifaces.cdi.param.ParamProducer.lambda$validateValues$1(ParamProducer.java:325)
  at deployment//org.omnifaces.cdi.param.ParamProducer.runWithSimulatedLabelAndValueOnViewRoot(ParamProducer.java:258)
  at deployment//org.omnifaces.cdi.param.ParamProducer.validateValues(ParamProducer.java:325)
  at deployment//org.omnifaces.cdi.param.ParamProducer.produce(ParamProducer.java:107)
  at deployment//org.omnifaces.cdi.param.ParamExtension$ParamInjectionTarget.inject(ParamExtension.java:123)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:161)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.util.bean.IsolatedForwardingBean.create(IsolatedForwardingBean.java:45)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.application.view.ViewScopeContextManager.createBean(ViewScopeContextManager.java:142)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.application.view.ViewScopeContext.get(ViewScopeContext.java:114)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.contexts.PassivatingContextWrapper$AbstractPassivatingContextWrapper.get(PassivatingContextWrapper.java:84)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:100)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:694)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.module.web.el.AbstractWeldELResolver.lookup(AbstractWeldELResolver.java:107)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.module.web.el.AbstractWeldELResolver.getValue(AbstractWeldELResolver.java:90)
  at org.jboss.as.jsf-injection//org.jboss.as.jsf.injection.weld.ForwardingELResolver.getValue(ForwardingELResolver.java:46)
  at javax.el.api@2.0.0.Final//javax.el.CompositeELResolver.getValue(CompositeELResolver.java:136)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:156)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:184)
  at org.glassfish.jakarta.el@3.0.3.jbossorg-2//com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:91)
  at org.glassfish.jakarta.el@3.0.3.jbossorg-2//com.sun.el.parser.AstValue.getBase(AstValue.java:125)
  at org.glassfish.jakarta.el@3.0.3.jbossorg-2//com.sun.el.parser.AstValue.getValue(AstValue.java:173)
  at org.glassfish.jakarta.el@3.0.3.jbossorg-2//com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:183)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.module.web.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
  at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.module.web.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:73)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:170)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:157)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.UIOutput.getValue(UIOutput.java:140)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.UIInput.getValue(UIInput.java:287)
  at deployment//org.primefaces.util.ComponentUtils.getValueToRender(ComponentUtils.java:104)
  at deployment//org.primefaces.util.ComponentUtils.getValueToRender(ComponentUtils.java:68)
  at deployment//org.primefaces.component.inputtext.InputTextRenderer.encodeMarkup(InputTextRenderer.java:96)
  at deployment//org.primefaces.component.inputtext.InputTextRenderer.encodeEnd(InputTextRenderer.java:67)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:595)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponent.encodeAll(UIComponent.java:1654)
  at javax.faces.api@3.0.0.SP04//javax.faces.render.Renderer.encodeChildren(Renderer.java:152)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:566)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponent.encodeAll(UIComponent.java:1647)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponent.encodeAll(UIComponent.java:1650)
  at javax.faces.api@3.0.0.SP04//javax.faces.component.UIComponent.encodeAll(UIComponent.java:1650)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:468)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:170)
  at javax.faces.api@3.0.0.SP04//javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:132)
  at javax.faces.api@3.0.0.SP04//javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:132)
  at javax.faces.api@3.0.0.SP04//javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:132)
  at deployment//org.omnifaces.viewhandler.OmniViewHandler.renderView(OmniViewHandler.java:155)
  at javax.faces.api@3.0.0.SP04//javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:132)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:102)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.lifecycle.Phase.doPhase(Phase.java:76)
  at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:199)
  at javax.faces.api@3.0.0.SP04//javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:708)
  at javax.faces.api@3.0.0.SP04//javax.faces.webapp.FacesServlet.service(FacesServlet.java:451)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
  at io.opentracing.contrib.opentracing-jaxrs2//io.opentracing.contrib.jaxrs2.server.SpanFinishingFilter.doFilter(SpanFinishingFilter.java:52)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
  at deployment//org.omnifaces.facesviews.FacesViewsForwardingFilter.filterExtensionLessToExtension(FacesViewsForwardingFilter.java:180)
  at deployment//org.omnifaces.facesviews.FacesViewsForwardingFilter.filterExtensionLess(FacesViewsForwardingFilter.java:137)
  at deployment//org.omnifaces.facesviews.FacesViewsForwardingFilter.filterExtensionLess(FacesViewsForwardingFilter.java:123)
  at deployment//org.omnifaces.facesviews.FacesViewsForwardingFilter.doFilter(FacesViewsForwardingFilter.java:84)
  at deployment//org.omnifaces.filter.HttpFilter.doFilter(HttpFilter.java:108)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
  at org.wildfly.security.elytron-web.undertow-server@1.8.0.Final//org.wildfly.elytron.web.undertow.server.ElytronRunAsHandler.lambda$handleRequest$1(ElytronRunAsHandler.java:68)
  at org.wildfly.security.elytron-private@1.14.1.Final//org.wildfly.security.auth.server.FlexibleIdentityAssociation.runAsFunctionEx(FlexibleIdentityAssociation.java:103)
  at org.wildfly.security.elytron-private@1.14.1.Final//org.wildfly.security.auth.server.Scoped.runAsFunctionEx(Scoped.java:161)
  at org.wildfly.security.elytron-private@1.14.1.Final//org.wildfly.security.auth.server.Scoped.runAs(Scoped.java:73)
  at org.wildfly.security.elytron-web.undertow-server@1.8.0.Final//org.wildfly.elytron.web.undertow.server.ElytronRunAsHandler.handleRequest(ElytronRunAsHandler.java:67)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:117)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
  at io.undertow.core@2.2.4.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  at io.undertow.core@2.2.4.Final//io.undertow.security.handlers.AuthenticationConstraintHandler.handleRequest(AuthenticationConstraintHandler.java:53)
  at io.undertow.core@2.2.4.Final//io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.security.ServletSecurityConstraintHandler.handleRequest(ServletSecurityConstraintHandler.java:59)
  at io.undertow.core@2.2.4.Final//io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
  at org.wildfly.security.elytron-web.undertow-server-servlet@1.8.0.Final//org.wildfly.elytron.web.undertow.server.servlet.CleanUpHandler.handleRequest(CleanUpHandler.java:38)
  at io.undertow.core@2.2.4.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
  at io.undertow.core@2.2.4.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.SendErrorPageHandler.handleRequest(SendErrorPageHandler.java:52)
  at io.undertow.core@2.2.4.Final//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:269)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:78)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:133)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:130)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1530)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:249)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:78)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:99)
  at io.undertow.core@2.2.4.Final//io.undertow.server.Connectors.executeRootHandler(Connectors.java:387)
  at io.undertow.core@2.2.4.Final//io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:841)
  at org.jboss.threads@2.4.0.Final//org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
  at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
  at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
  at org.jboss.threads@2.4.0.Final//org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
  at org.jboss.xnio@3.8.4.Final//org.xnio.XnioWorker$WorkerThreadFactory$1$1.run(XnioWorker.java:1280)
  at java.base/java.lang.Thread.run(Thread.java:834)

我正在OpenJDK 11.0上运行。9.1 1,WildFly 22.0。1.决赛,全方位3.10。1(如果有关系,则为10.0.0-RC1)

由于这个问题,我在@Param中添加了disableBeanValidation=true,并且没有抛出NPE。但这让我觉得我一开始做错了什么(而且上面的Javadoc有些过时)。此外,如果我使用自定义CDI拦截器(这对我来说不是一个选项),则根本不会抛出NPE。

如何妥善解决这一问题?我是做错了什么,还是这是个错误?

共有1个答案

邬博涉
2023-03-14

这是OmniFaces@Param中的一个错误,它从版本3.6开始在从@Param中删除@Inject需求的工作中表现出来。当托管bean被代理为拦截器目标时,它无法正常工作。托管bean的Object#getClass()返回代理类,并且BeanManager#解决()找不到Bean

已根据第624版进行了修复,该版本将在OmniFaces 3.11和4.0-M8中提供。

同时,一个解决办法是将@Inject添加回@Param

@Inject @Param(required = true)
private String token;

 类似资料:
  • 我试图在Karaf中定义和使用自定义运行时注释。它不适合我。 我可以将包配置为使用Pax CDI OpenWebBeans CDI容器。集装箱很好用。我可以看到公开的服务(standard annotation@OsgiServiceProvider)。 但对于已定义的@AroundInvoke方法的自定义拦截器标记,它不处理使用自定义注释方法进行注释的拦截器标记。 我使用apache-karaf

  • null 我尝试将@priority(interceptor.priority.platform_beform)和@prematching也放入我的过滤器中,但即使是在OIDC启动后也会调用。 另外,是否有任何方法支持扩展quarkus oidc逻辑以包括自定义代码? 我无法获得oidc和keycloak-auth拦截器的优先级(知道这些可以帮助我决定过滤器的优先级)。请帮忙。

  • 我已经创建了一个CDI(WELD)拦截器,它可以工作并拦截它应该拦截的内容。 其中MyInterceptorBinding是一个拦截器绑定: 我想将我的拦截器注入一个bean类,如下所示: 但是这个注入带来了一个错误: 我如何克服这个问题?问题是否与拦截器的事实有关?我应该使用CDI便携式扩展设施吗?如果是,如何解决?

  • 在RestTemplate中,我有一个自定义拦截器,它将记录一些请求-响应详细信息并保存到数据库。 我的自定义拦截器: springboot中的RestTemboard bean配置: 将拦截器添加到restTemboard bean: 如何将此拦截器添加到佯装客户端? 正在应用中。yml: InterceptorOne为假装客户端中的每个请求添加标头: 但是我不能添加日志服务拦截器,因为它由于错

  • 我根据JBoss文档创建了一个拦截器。 为了测试拦截器,我把: 现在,我想使用WeldJUnit4Runner类在单元测试中测试这个拦截器。 现在的预期产出当然是 但结果却是 主要问题是,如果我在测试中注入bean,这也是正确的:我调用的bean的第一个方法被拦截,但是如果这个方法调用另一个方法,拦截器就不会被调用。 任何想法都非常感谢! 我只是按照@adrobisch的建议修改了我的代码,效果如

  • 问题内容: 我知道如何拦截所有请求,但是我只想拦截来自我资源的请求。 有谁知道如何做到这一点? 问题答案: 如果只想拦截来自特定资源的请求,则可以使用可选的action 属性。Angular的文档请参见此处(用法>操作) 的JavaScript Plunker:http ://plnkr.co/edit/xjJH1rdJyB6vvpDACJOT?p=preview