我试图将Vaadin 12 PWA前端添加到现有的JAX-RS RESTful服务中,并使用@Inject将其注入Vaadin类中,但是我一直在获取Nullpointer异常。
现有服务具有/json/keystore
端点(请参见下面的类),并且Vaadin UI具有作为入口点/
。与Vaadin结合使用时,我无法再通过/json/keystore
访问以前的服务。
我正在使用最新的Vaadin CDI插件(10.0.2)和Thorntail 2.3.0及其CDI插件,该插件适用于先前存在的类,但是在使用Vaadin时不起作用。
有什么建议可遵循吗? 我应该如何将现有服务与Vaadin集成在一起,而又保留不同的路径?
我的Vaadin MainView 类如下:
package thorntail.elytron.wui;
import thorntail.elytron.core.controller.KeystoreStorageService;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.page.BodySize;
import com.vaadin.flow.component.page.Viewport;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.PWA;
import com.vaadin.flow.theme.Theme;
import com.vaadin.flow.theme.material.Material;
import org.jboss.logging.Logger;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
@Route( value = "" )
@BodySize( width = "100vw", height = "100vh" )
@Viewport( "width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes" )
@Theme( Material.class )
@PWA( name = "Keystore Manager WUI", shortName = "kmwui" )
public class MainView extends VerticalLayout
{
private static final Logger LOGGER = Logger.getLogger( MainView.class );
@Inject
private Instance<KeystoreStorageService> keystoreStorageServiceInstance;
public MainView()
{
LOGGER.info( "Initializing PWA" );
if( keystoreStorageServiceInstance.isUnsatisfied() )
{
LOGGER.warn( "KeystoreStorageService is null" );
}
final TextField nameInput = new TextField( "Name" );
Button submitBtn = new Button( "Submit" );
submitBtn.addClickListener( e -> Notification.show( nameInput.getValue() ) );
add( nameInput );
add( submitBtn );
}
}
我的 KeystoreStorageService 类在另一个Maven模块中如下所示:
@ApplicationScoped
public class KeystoreStorageService
{
private static final Logger LOGGER = Logger.getLogger( KeystoreStorageService.class );
@PersistenceContext( unitName = "keystore" )
private EntityManager em;
**
* @return all the {@link KeystoreModel} in the db
*/
public List<KeystoreModel> findAll()
{
LOGGER.info( "Finding all Keystores" );
return em.createNamedQuery( "Keystore.findAll", KeystoreModel.class ).getResultList();
}
//...other methods here
}
生成的堆栈跟踪,导航到我的PWA的主路径,如下所示:
There was an exception while trying to navigate to '' with the exception message 'Unable to create an instance of 'thorntail.elytron.wui.MainView'. The constructor threw an exception.'
java.lang.IllegalArgumentException: Unable to create an instance of 'thorntail.elytron.wui.MainView'. The constructor threw an exception.
at com.vaadin.flow.internal.ReflectTools.createProxyInstance(ReflectTools.java:510)
at com.vaadin.flow.internal.ReflectTools.createInstance(ReflectTools.java:447)
at com.vaadin.flow.di.DefaultInstantiator.getOrCreate(DefaultInstantiator.java:65)
at com.vaadin.cdi.CdiInstantiator.lambda$getOrCreate$2(CdiInstantiator.java:77)
at com.vaadin.cdi.BeanLookup.lookupOrElseGet(BeanLookup.java:89)
at com.vaadin.cdi.CdiInstantiator.getOrCreate(CdiInstantiator.java:76)
at com.vaadin.flow.di.Instantiator.createRouteTarget(Instantiator.java:158)
at com.vaadin.cdi.CdiInstantiator$Proxy$_$$_WeldClientProxy.createRouteTarget(Unknown Source)
at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.lambda$getRouteTarget$1(AbstractNavigationStateRenderer.java:116)
at java.base/java.util.Optional.orElseGet(Optional.java:369)
at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.getRouteTarget(AbstractNavigationStateRenderer.java:115)
at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.handle(AbstractNavigationStateRenderer.java:167)
at com.vaadin.flow.router.Router.handleNavigation(Router.java:221)
at com.vaadin.flow.router.Router.navigate(Router.java:192)
at com.vaadin.flow.router.Router.initializeUI(Router.java:95)
at com.vaadin.flow.server.BootstrapHandler.createAndInitUI(BootstrapHandler.java:1117)
at com.vaadin.flow.server.BootstrapHandler.synchronizedHandleRequest(BootstrapHandler.java:396)
at com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40)
at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1545)
at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:227)
at com.vaadin.cdi.CdiVaadinServlet.service(CdiVaadinServlet.java:65)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:791)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
at io.undertow.websockets.jsr.JsrWebSocketFilter.doFilter(JsrWebSocketFilter.java:173)
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.swarm.generated.FaviconErrorHandler.handleRequest(FaviconErrorHandler.java:61)
at io.undertow.server.handlers.PathHandler.handleRequest(PathHandler.java:94)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1502)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:360)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)
at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1985)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1487)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1349)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at com.vaadin.flow.internal.ReflectTools.createProxyInstance(ReflectTools.java:479)
... 70 more
Caused by: java.lang.NullPointerException
at thorntail.elytron.wui.MainView.<init>(MainView.java:52)
... 75 more
答案 0 :(得分:1)
我设法通过在Vaadin项目中添加beans.xml
并使用@Route(对于Vaadin)和@Application(对于jax-rs)设置路径来解决此问题
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd" bean-discovery-mode="all">
</beans>
默认情况下,如果使用io.thorntail:cdi分数,则thorntail不需要额外的bean.xml文件用于CDI,但是对于Vaadin来说,则是必需的