Here are the Maven dependencies for the code about to come. (This isn't the
whole pom.xml.) This was the latest version of HAPI-FHIR at the time
I wrote this. In particular, I'm guided by
HAPI-FHIR Documentation,
specifically, the link
Parsing and Serializing. All this HAPI-FHIR software was written
by or under the ægis of James Agnew—huge kudos to him.
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<hapi-fhir.version>4.2.0</hapi-fhir.version>
</properties>
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-base</artifactId>
<version>${hapi-fhir.version}</version>
</dependency>
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-structures-dstu3</artifactId>
<version>${hapi-fhir.version}</version>
</dependency>
</project>
package com.windofkeltia.fhir;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.IParser;
import org.hl7.fhir.dstu3.model.Patient;
import com.windofkeltia.utilities.TestUtilities;
/**
* Prime this pump: figure out how HAPI FHIR works from our direction: usually, consumers are,
* unlike us, interested in the client-side or in FHIR servers set up to validate what they
* produce. We're not here to produce FHIR (XML or JSON), but to parse it and turn it into
* a meta- or interlingua for a search engine (not shown here).
*
* @author Russell Bateman
* @since February 2020
*/
public class FhirContextTest
{
// @formatter:off
@Rule public TestName name = new TestName();
@Before public void setUp() { TestUtilities.setUp( name ); }
@After public void tearDown() { }
private static boolean VERBOSE = true;//TestUtilities.VERBOSE;
/**
* The purpose of this case is only to demonstrate the client side of things a little bit
* (see {@link FhirContextTest} documentation), which isn't what interests us, but doing
* this does give us an easy way to come up with the XML for a Patient. We could also
* generate the JSON for a sample Patient. Whatever we made in here, we could use in one
* of the more germane tests below.
*/
@Ignore
@Test
public void testXmlSerialization()
{
FhirContext context = FhirContext.forDstu3();
IParser parser = context.newXmlParser().setPrettyPrint( true );
Patient patient = new Patient();
patient.addName().setFamily( "Simpson" ).addGiven( "James" );
String serialized = parser.encodeResourceToString( patient );
System.out.println( " Patient (serialized):\n" + serialized );
}
@Test
public void testXml()
{
FhirContext context = FhirContext.forDstu3();
final String INPUT = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
+ "<Patient xmlns=\"http://hl7.org/fhir\">\n"
+ " <name>\n"
+ " <family value=\"Simpson\" />\n"
+ " <given value=\"James\" />\n"
+ " </name>\n"
+ "</Patient>";
if( VERBOSE )
System.out.println( " Input:\n" + INPUT );
IParser parser = context.newXmlParser();
Patient patient = parser.parseResource( Patient.class, INPUT );
if( VERBOSE )
{
System.out.print( " " + patient.getName().get( 0 ).getFamily() );
System.out.println( ", " + patient.getName().get( 0 ).getGiven() );
}
}
@Test
public void testJson()
{
FhirContext context = FhirContext.forDstu3();
final String INPUT = "{"
+ " \"resourceType\" : \"Patient\","
+ " \"name\" :"
+ " ["
+ " {"
+ " \"family\": \"Simpson\""
+ " }"
+ " ]"
+ "}";
if( VERBOSE )
System.out.println( " Input:\n " + INPUT );
IParser parser = context.newJsonParser();
Patient patient = parser.parseResource( Patient.class, INPUT );
if( VERBOSE )
{
System.out.print( " " + patient.getName().get( 0 ).getFamily() );
System.out.println( ", " + patient.getName().get( 0 ).getGiven() );
}
}
/**
* Start out in XML, but switch to JSON for output (serialization).
*/
@Test
public void testXmlToJson()
{
FhirContext context = FhirContext.forDstu3();
final String INPUT = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"
+ "<Patient xmlns=\"http://hl7.org/fhir\">"
+ " <name>"
+ " <family value=\"Simpson\" />"
+ " <given value=\"James\" />"
+ " </name>"
+ "</Patient>";
if( VERBOSE )
System.out.println( " Input:\n" + INPUT );
IParser parser = context.newXmlParser();
Patient patient = parser.parseResource( Patient.class, INPUT );
parser = context.newJsonParser().setPrettyPrint( true );
String serialized = parser.encodeResourceToString( patient );
if( VERBOSE )
System.out.println( " Pretty-printed JSON output:\n" + serialized );
}
}