FHIR Code in 10 Minutes
FHIR Code in 10 Minutes
This is a simple code to get started on FHIR with Java. Using HAPI FHIR library with an example of Patient Resource.
Join the DZone community and get the full member experience.Join For Free
You have read tons of articles on FHIR and the tell-tale signs have appeared on hairs on the back of your neck. Your gut tells you to get your feet wet (you got to admit that is funny, gut talking to feet) and ease out that angst. You may be an Architect (hands-on or not) or hands-on Engineer or may be rusty on coding or even deep into it, you could be coming from any such background but FHIR is the beast that you are looking to wrestle. If this speaks to you then let's work together for 10 minutes and then you can go back to the world of FHIR with a renewed goal like Robert the Bruce from the famous spider legend.
This code is for FHIR Facade pattern meaning that we are not going for full FHIR Restful Server implementation and our objective here is to only retrieve data. We will use Spring Boot just because those Booties are so awesome and simple. We will also use our friendly HAPI FHIR library to create our java application.
Tools: Postman, IntelliJ 2019.1 that's it.. ahem.. Java 8 installed on your machine. Everything else like HAPI, we will grab 'em as we go here.
Pre-requisite: None. It's assumed that you have an interest in FHIR and understand Java. Silly me, if not why would you read this far?
Take-Home: When you are done here, you will be able to create an FHIR Restful server and be able to search for patients based on the family name.
Enough prologue, I know times ticking so let's start the clock here.
1. In your first minute, we will set up your Spring Boot project in IntelliJ.
Load up your IntelliJ IDE and click on "New Project".
Select Spring Initializr on the left-hand side.
On the right side pane Let’s select the
Project SDK 1.8 and leave
“Default” for Choose Intializr Server URL.
Click on Next
In the next step enter as below.
Type: Maven Project
Java Version: 8
Name: FHIR Example
Description: My first FHIR project
Then click on Next
In this step from Developer Tools
Spring Boot DevTools
Lombok (Optional, but it will come in handy when you expand on this example)
From Web Select
From SQL Select
In this step enter a project name and location
Project Name: fhirexample
Project Location: c:\fhirexample (make sure this directory exists)
Click on Finish. Now your project is created and ready to dance on your music.
2. This is going to be our HAPI minute
Your Spring is ready but you are not quite FHIR ready yet. So In your second minute we will load up HAPI library. While there are many subprojects in FHIR Codebase we only need 3 for FHIR Façade or as James Agnew puts it, for HAPI Plain Server implementation.
So let's go ahead and add these dependencies in our project. You can follow through screenshots here.
Click on File->Project Structures.
Under Project, Settings click on Libraries
On the right-hand pane click on the “+” sign and select “From Maven”.
Then enter these libraries one after other
After you click Ok IntelliJ will ask you this
Just click Ok.
Note: This dialogue will reappear whenever you add a new library.
NOTE: If by any chance you had forgotten to select H2 Database while setting up Spring boot this will be the time to rectify that mistake by selecting
com.h2database:h2:1.4.200 from maven library just like the steps above.
Click Ok on this screen to close this window.
There We are in Minute 2.
Minute 3 relax. Never mind, you want to get to the finish line before its too late, and research says that the average attention span of the modern human is between 8 to 12 seconds. That means we are now competing with goldfish which has a span of 9 seconds. And it also means that I am already at the borrowed time, so let's get back on our horse and get this baby running.
3. This minute we will spend reviewing our current state. If you expand on your project and go down to fhirexample->src->main->java->com.example.fhirexample->fhirExampleApplication
Your screen looks would look like this with the main class created inside your IntelliJ. This would be a good time to check on .iml and pom file as well.
Note that we added all the dependency to .iml file and not pom. The difference is out of the scope of this discussion. But something for you to mull over.
The first code snippet is pom.xml and next one is for fhirexample.iml
4. Now we will start to code for FHIR.
Right-click on your package com.example.fhirexample and select New Class.
Enter Name: SimpleRestfulServer
Click on OK.
Copy this code into your class and pay attention to the Initialize method. What we are doing is setting FHIR Context (this is going to be expensive so set it once Read more about it ... Just not in while our clock is ticking :) ). This class is going to be the heart of your FHIR code and initialize method is going to be driving everything in your heart. Don’t drive her crazy though.
5. Now lets Setup Servlet Context. This is Spring Boot ABC. Open your FhirExampleApplication Class and copy the code below. Pay attention to the Bean Annotation. This is where we are bringing FHIR into play.
6. You are now ready to see results of your effort thus far and mark your first step into FHIR. So go ahead and click on Run FhirExampleApplication(From Run menu or button on top right or just press Shift+F10). Once your server has started, Open up your Postman and enter this URL http://localhost:8080/metadata. A capability statement will appear just like the screenshot below. Congratulations you are now FHIR enabled!
(True Fact: By mere publishing, a capability statement technically speaking you are FHIR capable now. You don’t have any resources exposed but you can claim that status.)
I have observed that the Server ate away 7-10 seconds of our time just to start. Hopefully, we will make up time somewhere by the end here.
7. Now its time to add a Patient resource and add meaning to your existence as an FHIR capable server. So right-click on your package com.example.fhirexample and from the context menu select
New-> Package. Enter package name as ResourceProvider, just so that you are organized and whole all your Resource class in one package.
This step would buy us back the 7 seconds we lost in our last step. But hey who is keeping count, right?
8. Inside Resource, package creates a class PatientResourceProvider (Right-click on ResourceProvider, then New->Class). copy this code into it.
- Now its time to activate this Resource Provider so go back to SimpleRestfulServer class and add this line to the end of the initialize method - You know where we have "Coming Soon" comment. It has now come!
Here is the whole code snipped just in case if you want to copy and paste the whole SimpleRestfulServer class.
Now its time to see your final results. Go ahead and click on Restart FhirExampleApplication. Once your server has started, open up your Postman and
http://localhost:8080/Patient?family=Chakravarty – This will return search results from our list for patients with last name Chakravarty. PS: One of the greatest actors of our time. Read more about Mithun Chakravarty.
But wait there is more A little bonus for you.
Enter this URL
Here you are reading the first patient on your list. This has been enabled because we added a @Read annotation in our PatientResourceProvider.
I am excluding screenshot here because that is going to be your badge of honor, please share those!
10. Congratulations you have reached the end with one minute to spare. Don’t take it back yet. Please spend this one minute in leaving a comment, suggestion, feedback thought, question, just about anything... EXCEPT for SILENCE.
FHIR standard is extensive and is in very early stages. There is a lot of energy and enthusiasm behind making this happen. It can be overwhelming for anyone. Sometimes you have to try it once as the first step towards comfort.
Even if you have never written a single line of code before, I feel this post will still help you set up with that first step. I hope you feel empowered now to go back out there and conquer the FHIR world. There are many concepts here that need further explanation. I will be more than happy to address those in Q and A.
Opinions expressed by DZone contributors are their own.