检索应用程序处理程序时的 NPE
NPE when retrieving application handler
使用 Dropwizard 1.3.17,我们检索 ServiceLocator:
ServiceLocator serviceLocator = ((ServletContainer) environment.getJerseyServletContainer()).getApplicationHandler().getServiceLocator();
当我们迁移到 Dropwizard 2 时,这变成了:
ServletContainer servletContainer = (ServletContainer) Objects.requireNonNull(environment.getJerseyServletContainer());
ApplicationHandler applicationHandler = servletContainer.getApplicationHandler();
InjectionManager injectionManager = applicationHandler.getInjectionManager();
ServiceLocator serviceLocator;
if (injectionManager instanceof ImmediateHk2InjectionManager)
{
serviceLocator = ((ImmediateHk2InjectionManager) injectionManager).getServiceLocator();
}
else if (injectionManager instanceof DelayedHk2InjectionManager)
{
serviceLocator = ((DelayedHk2InjectionManager) injectionManager).getServiceLocator();
}
else
{
throw new IllegalStateException("Expecting an HK2 injection manager");
}
但是,ApplicationHandler 为空。有什么想法吗?
我们重构了我们的代码,使其在 org.eclipse.jetty.server.Server.start 之前不再依赖服务定位器。似乎有效。
使用 Dropwizard 1.3.17,我们检索 ServiceLocator:
ServiceLocator serviceLocator = ((ServletContainer) environment.getJerseyServletContainer()).getApplicationHandler().getServiceLocator();
当我们迁移到 Dropwizard 2 时,这变成了:
ServletContainer servletContainer = (ServletContainer) Objects.requireNonNull(environment.getJerseyServletContainer());
ApplicationHandler applicationHandler = servletContainer.getApplicationHandler();
InjectionManager injectionManager = applicationHandler.getInjectionManager();
ServiceLocator serviceLocator;
if (injectionManager instanceof ImmediateHk2InjectionManager)
{
serviceLocator = ((ImmediateHk2InjectionManager) injectionManager).getServiceLocator();
}
else if (injectionManager instanceof DelayedHk2InjectionManager)
{
serviceLocator = ((DelayedHk2InjectionManager) injectionManager).getServiceLocator();
}
else
{
throw new IllegalStateException("Expecting an HK2 injection manager");
}
但是,ApplicationHandler 为空。有什么想法吗?
我们重构了我们的代码,使其在 org.eclipse.jetty.server.Server.start 之前不再依赖服务定位器。似乎有效。