I'm looking for a simple hello-world example of creating a custom Extension and linking its .url field to a StructureDefinition.
Background: we are using the HAPI FHIR v6.6.2 Plain Server and we expose the ResearchStudy FHIR resource, with base URL (for this example) hosted at
https://foo.com/fhir.
We would like to add a custom Extension to the ResearchStudy resource:
- Extension name = NumUniqueDatasets
- Type = int
- Cardinality: 0..1
Example response for /fhir/ResearchStudy/1:
{
"resourceType": "ResearchStudy",
"id": "1",
"extension": [ {
"url": "
https://foo.com/fhir/StructureDefinition/ResearchStudy-NumUniqueDatasets",
"valueInteger": 20
} ],
"status": "active"
...
}
I know that I need some Java code to create a new extension and add it to the study:
Extension ext = new Extension();
ext.setValue(new IntegerType(20));
myResearchStudy.addExtension(ext);
But the HAPI FHIR docs on Profiles and Extensions (
https://hapifhir.io/hapi-fhir/docs/model/profiles_and_extensions.html) don't give a more realistic example that actually shows how to serve up the StructureDefinition itself at
https://foo.com/fhir/StructureDefinition/ResearchStudy-NumUniqueDatasets.
How do I do this using HAPI FHIR? How do I create the StructureDefinition and tie my Extension instance back to it? Is there a way to auto-set the Extension URL without needing to explicitly call ext.setUrl()?