4

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

Vivek
  • 341
  • 1
  • 5
  • 15

1 Answers1

4

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.

CodeMonkey
  • 22,825
  • 4
  • 35
  • 75
  • do you have a documentation for implementing java RESTful client using hapi? Unfortunately the documents are not rich enough! – Jasmine Apr 12 '16 at 20:29
  • @JasonM1: Dear, I try to use size() and getResources() methods of Bundle class, but with R4 FHIR version don't exist. There are equivalent methods? – Joe Taras Apr 30 '20 at 14:47
  • Major redesign and refactoring over the years. Review the [javadoc](https://hapifhir.io/hapi-fhir/apidocs/hapi-fhir-structures-r4/undefined/org/hl7/fhir/r4/model/Bundle.html). Download R4 FHIR source package to find example code using Bundle. – CodeMonkey Apr 30 '20 at 15:12