Vivek
Vivek

Reputation: 345

Java REST client for FHIR data model

Could anybody give an example for Java REST client to search Patients using FHIR data model?

Upvotes: 4

Views: 7545

Answers (1)

CodeMonkey
CodeMonkey

Reputation: 23738

The FHIR HAPI Java API is a simple RESTful client API that works with FHIR servers.

Here's a simple code example that searches for all Patients at a given server then prints out their names.

 // Create a client (only needed once)
 FhirContext ctx = new FhirContext();
 IGenericClient client = ctx.newRestfulGenericClient("http://fhirtest.uhn.ca/base");

 // Invoke the client
 Bundle bundle = client.search()
         .forResource(Patient.class)
         .execute();

 System.out.println("patients count=" + bundle.size());
 List<Patient> list = bundle.getResources(Patient.class);
 for (Patient p : list) {
     System.out.println("name=" + p.getName());
 }

Calling execute() method above invokes the RESTful HTTP calls to the target server and decodes the response into a Java object.

The client abstracts the underlying wire format of XML or JSON with which the resources are retrieved. Adding one line to the client construction changes the transport from XML to JSON.

 Bundle bundle = client.search()
         .forResource(Patient.class)
         .encodedJson() // this one line changes the encoding from XML to JSON
         .execute();

Here's an example where you can constrain the search query:

Bundle response = client.search()
      .forResource(Patient.class)
      .where(Patient.BIRTHDATE.beforeOrEquals().day("2011-01-01"))
      .and(Patient.PROVIDER.hasChainedProperty(Organization.NAME.matches().value("Health")))
      .execute();

Likewise, you can use the DSTU Java reference library from HL7 FHIR website that includes the model API and a FhirJavaReferenceClient.

Upvotes: 4

Related Questions