hapifhir / hapi-fhir-jpaserver-starter

Apache License 2.0
379 stars 1.02k forks source link

NullPointerException Error when restarting server after adding implementation guide #509

Closed Yorigas closed 7 months ago

Yorigas commented 1 year ago

Hello,

I opened a thread on Google Groups and I've been asked to share the problem here. So here is a nice copy-paste of the Google thread!

Problem

I'm playing with implementation guides on the HAPI FHIR Starter server. I did not change anything in the server files but these lines in application.yaml :

hapi:
  fhir:
  (...)
  implementationguides:
      ch-core:
        name: ch.fhir.ig.ch-core
        version: 3.0.0
  (...)
  enable_repository_validating_interceptor: true

If I start the server the first time, there is no problem. It downloads resources and I can reach it on localhost:8080.

If I stop and restart the server, I get this error:

java.lang.RuntimeException: org.springframework.context.ApplicationContextException: Unable to start web server;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with
name 'hapiServletRegistration' defined in ca.uhn.fhir.jpa.starter.Application: Unsatisfied dependency expressed
through method 'hapiServletRegistration' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'restfulServer' defined in class path resource [ca/uhn/fhir/jpa/starter/common/StarterJpaConfig.class]:
Unsatisfied dependency expressed through method 'restfulServer' parameter 24; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'repositoryValidatingInterceptor' defined in class path resource [ca/uhn/fhir/jpa/starter/common/StarterJpaConfig.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [ca.uhn.fhir.jpa.interceptor.validation.RepositoryValidatingInterceptor]:
Factory method 'repositoryValidatingInterceptor' threw exception; nested exception is java.lang.NullPointerException:
Cannot invoke "java.lang.Integer.intValue()" because the return value of "ca.uhn.fhir.rest.api.server.IBundleProvider.size()" is null
    at org.eclipse.jetty.annotations.ServletContainerInitializersStarter.doStart (ServletContainerInitializersStarter.java:69)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:73)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext (ServletContextHandler.java:356)
    at org.eclipse.jetty.webapp.WebAppContext.startWebapp (WebAppContext.java:1449)
    at org.eclipse.jetty.maven.plugin.JettyWebAppContext.startWebapp (JettyWebAppContext.java:328)
    at org.eclipse.jetty.webapp.WebAppContext.startContext (WebAppContext.java:1414)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart (ContextHandler.java:916)
    at org.eclipse.jetty.servlet.ServletContextHandler.doStart (ServletContextHandler.java:288)
    at org.eclipse.jetty.webapp.WebAppContext.doStart (WebAppContext.java:524)
    at org.eclipse.jetty.maven.plugin.JettyWebAppContext.doStart (JettyWebAppContext.java:397)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:73)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start (ContainerLifeCycle.java:169)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart (ContainerLifeCycle.java:117)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart (AbstractHandler.java:97)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:73)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start (ContainerLifeCycle.java:169)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart (ContainerLifeCycle.java:117)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart (AbstractHandler.java:97)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:73)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start (ContainerLifeCycle.java:169)
    at org.eclipse.jetty.server.Server.start (Server.java:423)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart (ContainerLifeCycle.java:110)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart (AbstractHandler.java:97)
    at org.eclipse.jetty.server.Server.doStart (Server.java:387)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:73)
    at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.startJetty (AbstractJettyMojo.java:449)
    at org.eclipse.jetty.maven.plugin.AbstractJettyMojo.execute (AbstractJettyMojo.java:310)
    at org.eclipse.jetty.maven.plugin.JettyRunMojo.execute (JettyRunMojo.java:150)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:370)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:298)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:77)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:568)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)

It's reproducible: if I get the lastest version of the HAPI FHIR starter server and modify the application.yaml, I get this error after restarting.

The error seemed to come from the code in the buildUsingStoredStructureDefinitions method in RepositoryValidationInterceptorFactoryR4.java file (line 51) because results.size() is null.

I just added a condition before to check if results.size() is null... I'm not really sure why the results are null tho, but at least the server can restart!

securit commented 1 year ago

Same problem with implementationguides: nz: name: fhir.org.nz.ig.base version: 2.0.0

jamesagnew commented 1 year ago

FWIW it looks to me like the issue is that on RepositoryValidationInterceptorFactoryR4 (and the other version-specific classes) we should be calling setLoadSynchronousUpTo on the SearchParameterMap before initiating that search. No reason not to do a synchronous search in that case.

Sandeep-Rattu commented 1 year ago

Hi, Please tell me . How to call setLoadSynchronousUpTo before searching. I changed the code: IBundleProvider results = structureDefinitionResourceProvider.search(new SearchParameterMap().add(StructureDefinition.SP_KIND, new TokenParam("resource"))); if (results.size() != null){ Map<String, List> structureDefintions = results.getResources(0, results.size()) .stream() .map(StructureDefinition.class::cast) .collect(Collectors.groupingBy(StructureDefinition::getType));

    structureDefintions.forEach((key, value) -> {
        String[] urls = value.stream().map(StructureDefinition::getUrl).toArray(String[]::new);
        repositoryValidatingRuleBuilder.forResourcesOfType(key).requireAtLeastOneProfileOf(urls).and().requireValidationToDeclaredProfiles();
    });
    }

Server is running fine. But structure definition validations are not working. so please explain what to write...