{{announcement.body}}
{{announcement.title}}

FHIR Code in 10 Minutes

DZone 's Guide to

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.

· Java Zone ·
Free Resource

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.

Spring Initializr

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.

Group: com.example

Artifact: fhirexample

Type: Maven Project

Language: Java

Packaging: Jar

Java Version: 8

Version: 0.0.1-SNAPSHOT

Name: FHIR Example

Description: My first FHIR project

Package: com.example.fhirexample

project metadata

Then click on Next

In this step from Developer Tools

               Select

Spring Boot DevTools

Lombok (Optional, but it will come in handy when you expand on this example)

            From Web Select

                   Spring Web

             From SQL Select

                   H2 Database

new project sql

Click Next

               In this step enter a project name and location

               Project Name: fhirexample

               Project Location: c:\fhirexample (make sure this directory exists)

examples

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.

        Hapi-fhir-base

        Hapi-fhir-structures

        Hapi-fhir-jpaserver.base

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

ca.uhn.hapi.fhir:hapi-fhir-base:5.0.2

download waterbottle

After you click Ok IntelliJ will ask you this 

choose modules

Just click Ok

Note: This dialogue will reappear whenever you add a new library.

 ca.uhn.hapi.fhir:hapi-fhir-structures-r5:5.0.1

maven repo

hapi-fhir-jpaserver-base:5.0.2

maven repo

 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.

com.h2database:h2:1.4.200

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.

.ml pom file

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

XML
 




x
66


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4
    <modelVersion>4.0.0</modelVersion>
5
    <parent>
6
        <groupId>org.springframework.boot</groupId>
7
        <artifactId>spring-boot-starter-parent</artifactId>
8
        <version>2.3.1.RELEASE</version>
9
        <relativePath/> <!-- lookup parent from repository -->
10
    </parent>
11
    <groupId>com.example</groupId>
12
    <artifactId>fhirexample</artifactId>
13
    <version>0.0.1-SNAPSHOT</version>
14
    <name>FHIR Example</name>
15
    <description>My first FHIR project</description>
16
 
          
17
    <properties>
18
        <java.version>1.8</java.version>
19
    </properties>
20
 
          
21
    <dependencies>
22
        <dependency>
23
            <groupId>org.springframework.boot</groupId>
24
            <artifactId>spring-boot-starter-web</artifactId>
25
        </dependency>
26
 
          
27
        <dependency>
28
            <groupId>org.springframework.boot</groupId>
29
            <artifactId>spring-boot-devtools</artifactId>
30
            <scope>runtime</scope>
31
            <optional>true</optional>
32
        </dependency>
33
        <dependency>
34
            <groupId>com.h2database</groupId>
35
            <artifactId>h2</artifactId>
36
            <scope>runtime</scope>
37
        </dependency>
38
        <dependency>
39
            <groupId>org.projectlombok</groupId>
40
            <artifactId>lombok</artifactId>
41
            <optional>true</optional>
42
        </dependency>
43
        <dependency>
44
            <groupId>org.springframework.boot</groupId>
45
            <artifactId>spring-boot-starter-test</artifactId>
46
            <scope>test</scope>
47
            <exclusions>
48
                <exclusion>
49
                    <groupId>org.junit.vintage</groupId>
50
                    <artifactId>junit-vintage-engine</artifactId>
51
                </exclusion>
52
            </exclusions>
53
        </dependency>
54
    </dependencies>
55
 
          
56
    <build>
57
        <plugins>
58
            <plugin>
59
                <groupId>org.springframework.boot</groupId>
60
                <artifactId>spring-boot-maven-plugin</artifactId>
61
            </plugin>
62
        </plugins>
63
    </build>
64
 
          
65
</project>
66
 
          


XML
 




xxxxxxxxxx
1
95


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
  <component name="FacetManager">
4
    <facet type="Spring" name="Spring">
5
      <configuration />
6
    </facet>
7
    <facet type="web" name="Web">
8
      <configuration>
9
        <webroots />
10
        <sourceRoots>
11
          <root url="file://$MODULE_DIR$/src/main/java" />
12
          <root url="file://$MODULE_DIR$/src/main/resources" />
13
        </sourceRoots>
14
      </configuration>
15
    </facet>
16
  </component>
17
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
18
    <output url="file://$MODULE_DIR$/target/classes" />
19
    <output-test url="file://$MODULE_DIR$/target/test-classes" />
20
    <content url="file://$MODULE_DIR$">
21
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
22
      <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
23
      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
24
      <excludeFolder url="file://$MODULE_DIR$/target" />
25
    </content>
26
    <orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
27
    <orderEntry type="sourceFolder" forTests="false" />
28
    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.3.1.RELEASE" level="project" />
29
    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.3.1.RELEASE" level="project" />
30
    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.3.1.RELEASE" level="project" />
31
    <orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.3" level="project" />
32
    <orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.3" level="project" />
33
    <orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.13.3" level="project" />
34
    <orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.13.3" level="project" />
35
    <orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.30" level="project" />
36
    <orderEntry type="library" name="Maven: jakarta.annotation:jakarta.annotation-api:1.3.5" level="project" />
37
    <orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.26" level="project" />
38
    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.3.1.RELEASE" level="project" />
39
    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.11.0" level="project" />
40
    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.11.0" level="project" />
41
    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.11.0" level="project" />
42
    <orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.11.0" level="project" />
43
    <orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.0" level="project" />
44
    <orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.11.0" level="project" />
45
    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:2.3.1.RELEASE" level="project" />
46
    <orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.36" level="project" />
47
    <orderEntry type="library" name="Maven: org.glassfish:jakarta.el:3.0.3" level="project" />
48
    <orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:9.0.36" level="project" />
49
    <orderEntry type="library" name="Maven: org.springframework:spring-web:5.2.7.RELEASE" level="project" />
50
    <orderEntry type="library" name="Maven: org.springframework:spring-beans:5.2.7.RELEASE" level="project" />
51
    <orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.2.7.RELEASE" level="project" />
52
    <orderEntry type="library" name="Maven: org.springframework:spring-aop:5.2.7.RELEASE" level="project" />
53
    <orderEntry type="library" name="Maven: org.springframework:spring-context:5.2.7.RELEASE" level="project" />
54
    <orderEntry type="library" name="Maven: org.springframework:spring-expression:5.2.7.RELEASE" level="project" />
55
    <orderEntry type="library" scope="RUNTIME" name="Maven: org.springframework.boot:spring-boot-devtools:2.3.1.RELEASE" level="project" />
56
    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.3.1.RELEASE" level="project" />
57
    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.3.1.RELEASE" level="project" />
58
    <orderEntry type="library" scope="RUNTIME" name="Maven: com.h2database:h2:1.4.200" level="project" />
59
    <orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.12" level="project" />
60
    <orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:2.3.1.RELEASE" level="project" />
61
    <orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:2.3.1.RELEASE" level="project" />
62
    <orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test-autoconfigure:2.3.1.RELEASE" level="project" />
63
    <orderEntry type="library" scope="TEST" name="Maven: com.jayway.jsonpath:json-path:2.4.0" level="project" />
64
    <orderEntry type="library" scope="TEST" name="Maven: net.minidev:json-smart:2.3" level="project" />
65
    <orderEntry type="library" scope="TEST" name="Maven: net.minidev:accessors-smart:1.2" level="project" />
66
    <orderEntry type="library" scope="TEST" name="Maven: org.ow2.asm:asm:5.0.4" level="project" />
67
    <orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.30" level="project" />
68
    <orderEntry type="library" scope="TEST" name="Maven: jakarta.xml.bind:jakarta.xml.bind-api:2.3.3" level="project" />
69
    <orderEntry type="library" scope="TEST" name="Maven: jakarta.activation:jakarta.activation-api:1.2.2" level="project" />
70
    <orderEntry type="library" scope="TEST" name="Maven: org.assertj:assertj-core:3.16.1" level="project" />
71
    <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest:2.2" level="project" />
72
    <orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter:5.6.2" level="project" />
73
    <orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.6.2" level="project" />
74
    <orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" />
75
    <orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
76
    <orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.6.2" level="project" />
77
    <orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-params:5.6.2" level="project" />
78
    <orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.6.2" level="project" />
79
    <orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.6.2" level="project" />
80
    <orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:3.3.3" level="project" />
81
    <orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy:1.10.11" level="project" />
82
    <orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy-agent:1.10.11" level="project" />
83
    <orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.6" level="project" />
84
    <orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-junit-jupiter:3.3.3" level="project" />
85
    <orderEntry type="library" scope="TEST" name="Maven: org.skyscreamer:jsonassert:1.5.0" level="project" />
86
    <orderEntry type="library" scope="TEST" name="Maven: com.vaadin.external.google:android-json:0.0.20131108.vaadin1" level="project" />
87
    <orderEntry type="library" name="Maven: org.springframework:spring-core:5.2.7.RELEASE" level="project" />
88
    <orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.2.7.RELEASE" level="project" />
89
    <orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:5.2.7.RELEASE" level="project" />
90
    <orderEntry type="library" scope="TEST" name="Maven: org.xmlunit:xmlunit-core:2.7.0" level="project" />
91
    <orderEntry type="library" name="ca.uhn.hapi.fhir:hapi-fhir-base:5.0.2" level="project" />
92
    <orderEntry type="library" name="ca.uhn.hapi.fhir:hapi-fhir-structures-r5:5.0.1" level="project" />
93
    <orderEntry type="library" name="ca.uhn.hapi.fhir:hapi-fhir-jpaserver-base:5.0.2" level="project" />
94
  </component>
95
</module>



4. Now we will start to code for FHIR. 

Right-click on your package com.example.fhirexample and select New Class. 

Enter Name: SimpleRestfulServer

Kind: Class

simplerestfulserverClick 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. 

Java
 




x


1
package com.example.fhirexample;
2
 
          
3
import ca.uhn.fhir.context.FhirContext;
4
import ca.uhn.fhir.rest.server.RestfulServer;
5
 
          
6
import javax.servlet.ServletException;
7
import javax.servlet.annotation.WebServlet;
8
 
          
9
@WebServlet("/*")
10
public class SimpleRestfulServer extends RestfulServer{
11
    //Initialize
12
    @Override
13
    protected void initialize()throws ServletException{
14
        //create a context for the appropriate version
15
        setFhirContext(FhirContext.forR5());
16
        //Register Resource Providers - COMING SOON
17
 
          
18
    }
19
}
20
 
          



examples

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. 

Java
 




xxxxxxxxxx
1
21


 
1
package com.example.fhirexample;
2
 
          
3
import org.springframework.boot.SpringApplication;
4
import org.springframework.boot.autoconfigure.SpringBootApplication;
5
import org.springframework.boot.web.servlet.ServletRegistrationBean;
6
import org.springframework.context.annotation.Bean;
7
 
          
8
@SpringBootApplication
9
public class FhirExampleApplication {
10
 
          
11
    public static void main(String[] args) {
12
        SpringApplication.run(FhirExampleApplication.class, args);
13
    }
14
    @Bean
15
    public ServletRegistrationBean ServletRegistrationBean(){
16
        ServletRegistrationBean registration=new ServletRegistrationBean(new SimpleRestfulServer(),"/*");
17
        registration.setName("FhirServlet");
18
        return registration;
19
    }
20
}
21
 
          



code snippet

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!

postman

(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.

resourceprovider

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.

patientresourceprovider

Java
 




xxxxxxxxxx
1
121


 
1
package com.example.fhirexample.ResourceProvider;
2
 
          
3
import ca.uhn.fhir.rest.annotation.IdParam;
4
import ca.uhn.fhir.rest.annotation.Read;
5
import ca.uhn.fhir.rest.annotation.RequiredParam;
6
import ca.uhn.fhir.rest.annotation.Search;
7
import ca.uhn.fhir.rest.param.StringParam;
8
import ca.uhn.fhir.rest.server.IResourceProvider;
9
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
10
import org.hl7.fhir.dstu2.model.IdType;
11
import org.hl7.fhir.instance.model.api.IBaseResource;
12
import org.hl7.fhir.r5.model.Patient;
13
 
          
14
import java.util.ArrayList;
15
import java.util.HashMap;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.stream.Collectors;
19
 
          
20
public class PatientResourceProvider implements IResourceProvider {
21
    /**
22
     * Constructor
23
     */
24
    public PatientResourceProvider() {    }
25
    //A Map to hold all the patients.
26
    private Map<String, Patient> patientMap = new HashMap<String, Patient>();
27
 
          
28
    /* You want to be a law abiding class so please
29
     Implement abstract method getResourceProvider() from IResourceProvider
30
     */
31
    @Override
32
    public Class<? extends IBaseResource> getResourceType() {
33
        return Patient.class;
34
    }
35
    //Create a Map to hold patients data
36
 
          
37
    /**
38
     * The "@Search" annotation indicates that this method supports the
39
     * search operation. Similarly we can have more Search annotations defined
40
     * they can each take different parameters
41
     * Documentation just gets added to your Capability Statement
42
     *
43
     * @param theFamilyName
44
     *    This operation takes one parameter which is the search criteria. It is
45
     *    annotated with the "@Required" annotation. This annotation takes one argument,
46
     *    a string containing the name of the search criteria. The datatype here
47
     *    is StringParam, but there are other possible parameter types depending on the
48
     *    specific search criteria.
49
     *    There is a "@Optional" tag which can be used if you want to have a search criteria as optional
50
     * @return
51
     *    This method returns a list of Patients. This list may contain multiple
52
     *    matching resources, or it may also be empty.
53
     *    This annotation takes a "name" parameter which specifies the parameter's name (as it will appear in the search URL).
54
     *    FHIR defines standardized parameter names for each resource, and these are available as constants on the individual
55
     *    HAPI resource classes.
56
     */
57
 
          
58
    @Search
59
    public List<Patient> search(@RequiredParam(name = Patient.SP_FAMILY) StringParam theParam) {
60
        List<Patient> patients = new ArrayList<Patient>();
61
        patients = this.searchByFamilyName(theParam.getValue());
62
        return patients;
63
    }
64
 
          
65
    /**
66
     * Simple implementation of the "read" method
67
     * The "@Read" annotation indicates that this method supports the
68
     *     * read operation. Read operations should return a single resource
69
     *     * instance.
70
     *
71
     * This method will support a query like this http://localhost:8080/Patient/1
72
     */
73
    @Read()
74
    public Patient read(@IdParam IdType theId) {
75
        loadDummyPatients();
76
        Patient retVal = patientMap.get(theId.getIdPart());
77
        if (retVal == null) {
78
            throw new ResourceNotFoundException(theId);
79
        }
80
        return retVal;
81
    }
82
 
          
83
 
          
84
    private List<Patient> searchByFamilyName(String familyName){
85
        List<Patient> retPatients;
86
        loadDummyPatients();
87
        // Encode the output, including the narrative - see below
88
        // Loop through the patients looking for matches
89
        retPatients = patientMap.values()
90
                .stream()
91
                .filter(next -> familyName.toLowerCase().equals(next.getNameFirstRep().getFamily().toLowerCase()))
92
                .collect(Collectors.toList());
93
        return retPatients;
94
    }
95
 
          
96
    private void loadDummyPatients() {
97
        Patient patient = new Patient();
98
 
          
99
        patient.setId("1");
100
        patient.addIdentifier().setSystem("http://optum.com/MRNs").setValue("007");
101
        patient.addName().setFamily("Chakravarty").addGiven("Mithun").addGiven("A");
102
        patient.addAddress().addLine("Address Line 1");
103
        patient.addAddress().setCity("Mumbai");
104
        patient.addAddress().setCountry("India");
105
        patient.addTelecom().setValue("111-111-1111");
106
        this.patientMap.put("1", patient);
107
 
          
108
        for (int i = 2; i < 5; i++) {
109
            patient = new Patient();
110
            patient.setId(Integer.toString(i));
111
            patient.addIdentifier().setSystem("http://optum.com/MRNs").setValue("007" + i);
112
            patient.addName().setFamily("Bond" + i).addGiven("James").addGiven("J");
113
            patient.addAddress().addLine("House Line " + i);
114
            patient.addAddress().setCity("Your City");
115
            patient.addAddress().setCountry("USA");
116
            this.patientMap.put(Integer.toString(i), patient);
117
        }
118
    }
119
 
          
120
}
121
 
          



  1. 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!

registerProvider(new PatientResourceProvider());

registerprovider

Here is the whole code snipped just in case if you want to copy and paste the whole SimpleRestfulServer class.

Java
 




xxxxxxxxxx
1
23


 
1
package com.example.fhirexample;
2
 
          
3
import ca.uhn.fhir.context.FhirContext;
4
import ca.uhn.fhir.rest.server.RestfulServer;
5
import com.example.fhirexample.ResourceProvider.PatientResourceProvider;
6
 
          
7
import javax.servlet.ServletException;
8
import javax.servlet.annotation.WebServlet;
9
 
          
10
@WebServlet("/*")
11
public class SimpleRestfulServer extends RestfulServer{
12
    //Initialize
13
    @Override
14
    protected void initialize()throws ServletException{
15
        //createacontextfortheappropriateversion
16
        setFhirContext(FhirContext.forR5());
17
        //Registerresourceproviders
18
        registerProvider(new PatientResourceProvider());
19
 
          
20
    }
21
}



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 

Enter URL

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. 

That's it! 

But wait there is more A little bonus for you. 

Enter this URL

http://localhost:8080/Patient/1 -

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.

Conclusion

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.

Good luck!

Topics:
fhir, hapi fhir, intellij, java, java 8, postman, spring boot

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}