FHIR with SpringBoot - how autowire the fhir context

673 views
Skip to first unread message

Pablo

unread,
Nov 25, 2022, 5:56:53 AM11/25/22
to HAPI FHIR
Hi all.

I have just started to work with Hapi FHIR. 

We are trying to build a POC with Hapi FHIR and Spring Boot. We have read that the Fhir context is designed to be created once and reused.

In our FhirRestfulServer class we are setting the FhirContext.forR4(), but we want to reuse this context in a service class to call Fhir Client. How can we reuse this context set in a @Service class?

@WebServlet(urlPatterns = { "/fhir/*" }, displayName = "FHIR Server")
public class FhirRestfulServer extends RestfulServer
{
private final ApplicationContext applicationContext;

FhirRestfulServer(ApplicationContext context)
{
this.applicationContext = context;
}

@Override
protected void initialize() throws ServletException
{
super.initialize();
//create a context for the appropriate version
setFhirContext(FhirContext.forR4());
setDefaultPrettyPrint(true);
//Register Resource Providers
setResourceProviders(Arrays.asList(
applicationContext.getBean(CommunicationProvider.class),
applicationContext.getBean(DeviceProvider.class),
applicationContext.getBean(DeviceMetricProvider.class),
applicationContext.getBean(SpecimenProvider.class)));
// Now register the interceptor
OpenApiInterceptor openApiInterceptor = new OpenApiInterceptor();
registerInterceptor(openApiInterceptor);
}

}

Thanks in advance 

Panayiotis Savva

unread,
Nov 26, 2022, 6:24:03 AM11/26/22
to HAPI FHIR
Have a look at the starter project which is HapiFhir with Spring boot


I suggest you use this as a basis.

James Agnew

unread,
Nov 27, 2022, 12:30:41 PM11/27/22
to Panayiotis Savva, HAPI FHIR
One other thing I'll add - You can call FhirContext.getR4Cached() which will always return the same instance, so it's OK to call that from multiple places and won't result in multiple contexts.

Cheers,
James

--
You received this message because you are subscribed to the Google Groups "HAPI FHIR" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hapi-fhir+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/hapi-fhir/85f32d7e-1941-4558-a256-ef2df6dca7c8n%40googlegroups.com.

Pablo

unread,
Nov 27, 2022, 1:27:35 PM11/27/22
to James Agnew, Panayiotis Savva, HAPI FHIR
Thansk for your quick response, I have just look for FhirContext.getR4Cached() method in the api and I can found it!!


You received this message because you are subscribed to a topic in the Google Groups "HAPI FHIR" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/hapi-fhir/8xuGxQODkRc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to hapi-fhir+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/hapi-fhir/CAKowd8nfsdzSbYSmqn4oDABPM1Z_vJWi322vNvy-1o7dk-pbnA%40mail.gmail.com.

James Agnew

unread,
Nov 27, 2022, 1:30:54 PM11/27/22
to Pablo, Panayiotis Savva, HAPI FHIR
That probably means you have an old version of HAPI on your classpath.

Try executing "mvn dependency:tree" and examine the results to see what version is getting pulled in from where.

Cheers,
James

On Sun, Nov 27, 2022 at 1:29 PM Pablo <pablo...@gmail.com> wrote:

Pablo

unread,
Nov 27, 2022, 1:51:17 PM11/27/22
to James Agnew, Panayiotis Savva, HAPI FHIR
As you can see we have configured 6.2.1 to hapi-fhir-base, hapi-fhir-structures-r4, hapi-fhir-server and hapi-fhir-server-openapi. As far as I understand it is the latest version.

I have attached the dependencies.

What is the version that we should use?

Thanks in advance
dependency.txt

James Agnew

unread,
Nov 27, 2022, 2:17:05 PM11/27/22
to Pablo, Panayiotis Savva, HAPI FHIR
That looks correct to me. The FhirContext#forR4Cached() method should definitely be there. This might be an IDE issue where it's caching an old version? try building with Maven and see if it accepts that method.

Pablo

unread,
Nov 27, 2022, 2:31:06 PM11/27/22
to James Agnew, Panayiotis Savva, HAPI FHIR
Ahhh Ok, was an typo issue!! I understood that the method was called getR4Cached!! 

I will use the forR4Cached() method. Thanks a lot!!

Pablo

unread,
Dec 2, 2022, 2:48:30 PM12/2/22
to James Agnew, Panayiotis Savva, HAPI FHIR
El dom, 27 nov 2022 a las 19:27, Pablo (<pablo...@gmail.com>) escribió:
Reply all
Reply to author
Forward
0 new messages