Tinder
According to the FHIR specification, any conformant server must be able to export a Conformance statement, which indicates all of the resource types and potential operations that the server supports.
HAPI provides a Maven plugin called "Tinder" which is able to automatically generate a client based on that conformance statement.
Client Generation
The following example shows a simple Maven plugin which builds a client for the Health Intersections reference server.
Note that as of HAPI 0.8, you need to add a dependency to the plugin containing the version of FHIR you are building custom structures against.
<plugins>
<plugin>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-tinder-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals><goal>generate-structures</goal></goals>
<configuration>
<clientClassName>ca.uhn.hitest.HiTest</clientClassName>
<serverBaseHref>http://fhir.healthintersections.com.au/open</serverBaseHref>
<generateSearchForAllParams>true</generateSearchForAllParams>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-structures-dstu</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
This example will create a class called ca.uhn.hitest.HiTest which has methods to invoke the various server operations.
It is then possible to use this client as simply as:
// Instantiate the client
FhirContext ctx = new FhirContext(Patient.class);
IRestfulClientFactory clientFactory = ctx.newRestfulClientFactory();
ClientClass client = clientFactory.newClient(ClientClass.class, "http://fhir.healthintersections.com.au/open");
// Invoke the client
Bundle bundle = client.searchPatientByGender(new StringDt("M"));
System.out.println(bundle.getEntries().size());
// Prints: 50
Patient patientOne = (Patient) bundle.getEntries().get(0).getResource();
System.out.println(patientOne.getName().get(0).getFamily().get(0).getValue());
// Prints: Chalmers