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

How to properly resolve NPE when using OmniFaces @Param on bean with custom CDI interceptor

我想在使用自定义 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(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.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(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.access0(ServletInitialHandler.java:78)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletInitialHandler.call(ServletInitialHandler.java:133)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletInitialHandler.call(ServletInitialHandler.java:130)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.core.ServletRequestContextThreadSetupAction.call(ServletRequestContextThreadSetupAction.java:48)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.core.ContextClassLoaderSetupAction.call(ContextClassLoaderSetupAction.java:43)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=13=](UndertowDeploymentInfoService.java:1530)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=13=](UndertowDeploymentInfoService.java:1530)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=13=](UndertowDeploymentInfoService.java:1530)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=13=](UndertowDeploymentInfoService.java:1530)
  at org.wildfly.extension.undertow@22.0.1.Final//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=13=](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[=13=]0(ServletInitialHandler.java:78)
  at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletInitialHandler.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.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.run(XnioWorker.java:1280)
  at java.base/java.lang.Thread.run(Thread.java:834)

我 运行 使用 OpenJDK 11.0.9.1+1、WildFly 22.0.1.Final、OmniFaces 3.10.1(如果重要的话 PrimeFaces 10.0.0-RC1)

因为 我添加了 disableBeanValidation = true 到 @Param 并且 NPE 没有被抛出。但这感觉就像我首先做错了什么(而且关于这个的 Javadoc 有点过时了)。 另外,如果我省略了自定义 CDI 拦截器(这对我来说不是一个选项),则根本不会抛出 NPE。

如何正确解决这个问题?我做错了什么或者这是一个错误,也许?

这是 OmniFaces @Param 中的一个错误,从 3.6 版开始在从 @Param 中删除 @Inject 要求的过程中出现。当托管 bean 被代理为拦截器目标时,它无法正常工作。托管 bean 的 Object#getClass() 返回代理 class 并且 BeanManager#resolve() 找不到此代理 class 的 Bean<X> 并返回 null .这种意外情况最终导致在执行 bean 验证时 NullPointerException

fixed as per issue 624 将在 OmniFaces 3.11 和 4.0-M8 中可用。

同时,解决方法是将 @Inject 添加回您的 @Param

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