HL7 FHIR Converter
Beginning in HAPI FHIR 2.3, a new experimental feature called hapi-fhir-converter has been added to the project. This is an experimental feature so use it with caution!
This feature allows automated conversion from earlier versions of the FHIR structures to a later version.
The following page shows some basic examples. Please get in touch if you are able to contribute better examples!
Importing the Module
To use the hapi-fhir-converter module, import the following dependency into your project pom.xml (or equivalent)
<dependency> <groupId>ca.uhn.hapi.fhir</groupId> <artifactId>hapi-fhir-converter</artifactId> <version>2.4</version> </dependency>
Converting from DSTU2 to DSTU3
The following example shows a conversion from a hapi-fhir-structures-hl7org-dstu2 structure to a hapi-fhir-structures-dstu3 structure.
// Create a converter
VersionConvertorAdvisor advisor = new NullVersionConverterAdvisor();
VersionConvertor_10_30 converter = new VersionConvertor_10_30(advisor);
// Create an input resource to convert
org.hl7.fhir.instance.model.Observation input = new org.hl7.fhir.instance.model.Observation();
input.setEncounter(new org.hl7.fhir.instance.model.Reference("Encounter/123"));
// Convert the resource
org.hl7.fhir.dstu3.model.Observation output = converter.convertObservation(input);
String context = output.getContext().getReference();
Converting from DSTU2.1 to DSTU3
The following example shows a conversion from a hapi-fhir-structures-dstu2.1 structure to a hapi-fhir-structures-dstu3 structure.
// Create a resource to convert
org.hl7.fhir.dstu2016may.model.Questionnaire input = new org.hl7.fhir.dstu2016may.model.Questionnaire();
input.setTitle("My title");
// Convert the resource
org.hl7.fhir.dstu3.model.Questionnaire output = VersionConvertor_14_30.convertQuestionnaire(input);
String context = output.getTitle();