Hapi Fhir DomainResource, What URL do you use?

一曲冷凌霜 提交于 2019-12-06 02:52:31

Ok.

So the CustomResource still needs its own IResourceProvider. (thanks to daniels in the comments of the original question)

Here is a basic working example.

You'll do everything I listed in the original question AND you'll make and register an IResourceProvider for the new customresource.

new IResourceProvider

package mystuff.resourceproviders;

import org.hl7.fhir.dstu3.model.DateType;
import org.hl7.fhir.dstu3.model.IdType;
import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.Read;
import ca.uhn.fhir.rest.server.IResourceProvider;
import mystuff.CustomDatatype;
import mystuff.CustomResource;
import org.hl7.fhir.dstu3.model.DateTimeType;
import org.hl7.fhir.dstu3.model.StringType;
import org.hl7.fhir.instance.model.api.IBaseResource;

import java.util.Date;

public class CustomResourceProvider implements IResourceProvider {


    @Override
    public Class<? extends IBaseResource> getResourceType() {
        return CustomResource.class;
    }


    /* the IdType (datatype) will be different based on STU2 or STU3.  STU3 version below */
    @Read()
    public CustomResource getResourceById(@IdParam IdType theId) {
        // Now let's create an instance of our custom resource type
        // and populate it with some data
        CustomResource res = new CustomResource();
        res.setId(theId);

        // Add some values, including our custom datatype
        DateType value0 = new DateType("2015-01-01");
        res.getTelevision().add(value0);

        CustomDatatype value1 = new CustomDatatype();
        value1.setDate(new DateTimeType(new Date()));
        value1.setKittens(new StringType("FOO"));
        res.getTelevision().add(value1);

        res.setDogs(new StringType("Some Dogs"));
        return res;
    }
}

then you'll register this (as documented here):

http://hapifhir.io/doc_rest_server.html#_toc_create_a_server

instead of this:

   @Override
   protected void initialize() throws ServletException {
      /*
       * The servlet defines any number of resource providers, and
       * configures itself to use them by calling
       * setResourceProviders()
       */
      List<IResourceProvider> resourceProviders = new ArrayList<IResourceProvider>();
      resourceProviders.add(new RestfulPatientResourceProvider());
      resourceProviders.add(new RestfulObservationResourceProvider());
      setResourceProviders(resourceProviders);
   }

you'll have something like this

   @Override
   protected void initialize() throws ServletException {
      /*
       * The servlet defines any number of resource providers, and
       * configures itself to use them by calling
       * setResourceProviders()
       */
      List<IResourceProvider> resourceProviders = new ArrayList<IResourceProvider>();
      resourceProviders.add(new CustomResourceProvider());
      setResourceProviders(resourceProviders);
   }

URL for testing this (most probable local development url that is)

http://127.0.0.1:8080/fhir/CustomResource/12345

and you'll get back this JSON response.

{
    "resourceType": "CustomResource",
    "id": "12345",
    "meta": {
        "profile": [
            "http://hl7.org/fhir/profiles/custom-resource"
        ]
    },
    "televisionDate": [
        "2015-01-01"
    ],
    "televisionCustomDatatype": [
        {
            "date": "2019-01-14T11:49:44-05:00",
            "kittens": "FOO"
        }
    ],
    "dogs": "Some Dogs"
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!