jerseydropwizardhk2

NPE when retrieving application handler


Using Dropwizard 1.3.17, we retrieve the ServiceLocator as such:

ServiceLocator serviceLocator = ((ServletContainer) environment.getJerseyServletContainer()).getApplicationHandler().getServiceLocator();

When we migrated to Dropwizard 2, this became:

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");
}

However, ApplicationHandler is null. Any ideas?


Solution

  • We refactored our code not to rely on the service locator prior to org.eclipse.jetty.server.Server.start. Seems to be working.