【问题标题】:Hapi Fhir DomainResource, What URL do you use?Hapi Fhir DomainResource,您使用什么 URL?
【发布时间】:2019-01-11 20:23:20
【问题描述】:

http://hapifhir.io/doc_custom_structures.html

本文讨论了域资源。

但在某些情况下,您可能想要完全创建一个 自定义资源类型。只有在没有 其他选项,因为这意味着您正在创建一个资源类型 将无法与其他 FHIR 实现互操作。

我已经逐字实现了代码。 (我展示了下面的类(为了简洁,没有“胆量”)(完整代码在 url))

/**
 * This is an example of a custom resource that also uses a custom
 * datatype.
 * 
 * Note that we are extensing DomainResource for an STU3
 * resource. For DSTU2 it would be BaseResource. 
 */
@ResourceDef(name = "CustomResource", profile = "http://hl7.org/fhir/profiles/custom-resource")
public class CustomResource extends DomainResource {
}

/**
 * This is an example of a custom datatype. 
 * 
 * This is an STU3 example so it extends Type and implements ICompositeType. For
 * DSTU2 it would extend BaseIdentifiableElement and implement ICompositeDatatype.
 */
@DatatypeDef(name="CustomDatatype")
public class CustomDatatype extends Type implements ICompositeType {
}

我已经在我的代码库中“注册”了它:

        if (null != this.fhirContext)
        {
            this.fhirContext.registerCustomType(CustomResource.class);
            this.fhirContext.registerCustomType(CustomDatatype.class);
        }

(~尝试按照上面网址中的说明进行操作)

// Create a context. Note that we declare the custom types we'll be using
// on the context before actually using them
FhirContext ctx = FhirContext.forDstu3();
ctx.registerCustomType(CustomResource.class);
ctx.registerCustomType(CustomDatatype.class);

// Now let's create an instance of our custom resource type
// and populate it with some data
CustomResource res = new CustomResource();

// 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"));

// Now let's serialize our instance
String output = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(res);
System.out.println(output);

但这看起来像是控制台应用程序对两个对象的使用......而不是如何将其连接到 fhir-server。

我已经尝试了 3 个小时来确定要使用的 URL。

我尝试过的一些事情:

http://127.0.0.1:8080/fhir/CustomResource

http://127.0.0.1:8080/fhir/profiles/custom-resource

http://127.0.0.1:8080/fhir/custom-resource

无济于事...

网址是什么?

我如何填充它的值?

【问题讨论】:

  • 创建资源默认不会创建 URL。您是否设置并配置了 RESTful 服务器?如果是这样,您可以在此处添加对自定义资源和类型 hapifhir.io/doc_rest_server.html 的访问权限。
  • 是的,我让我的 fhir 服务器使用默认资源,例如 Patient 等
  • fhirContext 是“我的”代码...我在其中注册项目...因此我认为 URL 应该可以工作
  • 要将资源注册到 REST 服务器,您需要为您的自定义资源创建 ResourceProvider 类,并将该 ResourceProvider 注册到服务器,如我提供的初始链接中所述,如果您使用的是示例 JPA 服务器,则此处hapifhir.io/doc_jpa.html 概述的默认资源已完成工作,但您仍需要为自己的自定义资源添加。
  • 谢谢丹尼尔斯。现在我懂了”。我仍然需要为新的自定义资源创建一个 IResourceProvider ......并在那里添加一些方法。所以这是一个“呃”的时刻。我希望文档只是添加了一点点。有时你在树上那么远,看不到森林。我很快就会发布我的基本工作示例。

标签: hl7-fhir hapi-fhir


【解决方案1】:

好的。

所以 CustomResource 仍然需要它自己的 IResourceProvider。 (感谢原始问题的 cmets 中的丹尼尔斯)

这是一个基本的工作示例。

您将完成我在原始问题中列出的所有操作,并且您将为新的 customresource 创建并注册一个 IResourceProvider。

新的 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;
    }
}

然后您将进行注册(如此处所述):

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

而不是这个:

   @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);
   }

你会有这样的东西

   @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(最可能的本地开发 URL)

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

你会得到这个 JSON 响应。

{
    "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"
}

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 2020-12-07
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多