【问题标题】:Setting up a FHIR server设置 FHIR 服务器
【发布时间】:2018-04-11 06:55:22
【问题描述】:

我正在尝试使用 asp.net 核心 (c#) 在我的 localhost(概念验证)环境中设置一个小型服务器,但是当我访问“get”路由时仍然出现相同的错误:

FormatException: Cannot determine type of resource to create from json input data. Is there a 'resourceType' member present? (at path 'line 1, pos 1')

这是我的代码:

[Route("Patient/rvrbk")]
public Patient ServeClient()
{
    Patient patient = new Patient();

    patient.Id = "1";
    patient.Gender = AdministrativeGender.Male;
    patient.Name = new List<HumanName> { new HumanName { Family = "Verbeek", Given = new List<string> { "Rik" }, Suffix = new List<string> { "The Small" } } };
    patient.BirthDate = "2004-03-10";
    patient.Active = true;

    return patient;
}

[Route("get")]
public Patient GetClient()
{
    FhirClient client = new FhirClient("http://localhost:54240/");

    var patient = client.Read<Patient>("Patient/rvrbk");

    return patient;
}

我尝试在患者对象上设置 resourceType,但由于它是只读的,因此会产生错误。我确定我错过了一些微不足道的东西,但不知道是什么。

编辑

在@AdrianoRepetti 提供答案后,我想出了以下设置,但遇到了另一个错误:

FhirOperationException: Operation was unsuccessful, and returned status OK. OperationOutcome: Overall result: FAILURE (1 errors and 0 warnings) [ERROR] (no details)(further diagnostics: Endpoint returned a body with contentType 'text/plain', while a valid FHIR xml/json body type was expected. Is this a FHIR endpoint?).

代码:

[Route("Patient/rvrbk")]
public string ServeClient()
{
    Patient patient = new Patient();

    patient.Id = "1";
    patient.Gender = AdministrativeGender.Male;
    patient.Name = new List<HumanName> { new HumanName { Family = "Verbeek", Given = new List<string> { "Rik" }, Suffix = new List<string> { "The Small" } } };
    patient.BirthDate = "2004-03-10";
    patient.Active = true;

    FhirJsonSerializer serializer = new FhirJsonSerializer();

    string json = serializer.SerializeToString(patient);

    return json;
}

【问题讨论】:

  • @AdrianoRepetti 感谢您的回复,我不确定如何阅读您的回复,您所说的“同时发布名称”是什么意思?你能提供一些示例代码吗?我正在使用 STU3 实现。也许 Family 属性有一些重载?,这个版本的编译器没问题。
  • 您应该询问谁编写了您正在使用的 FHIR 实现,但是......有根据的猜测:反序列化器需要知道确切的对象类型才能确定必须实例化哪个类(给出 @987654326 的列表@ 实例应该是Derived 还是AnotherDerived?)。为什么这个?因为ServeClient() 返回一个简单的JSON 对象,其中resourceType 成员不存在。您可以指示您的 JSON 序列化程序添加它,或者 - 也许 - 使用您的 FHIR 实现提供的工具。
  • 是的,这个信息确实丢失了,我尝试询问作者如何将这个信息发送给客户。我会及时通知你的。
  • 查看,例如,合适的作者:github.com/ewoutkramer/fhir-net-api/blob/master/src/…。您不能简单地返回一个默认的序列化 JSON 对象。
  • 好的,我对代码进行了一些更改(参见原始帖子),但收到错误“FhirOperationException:操作不成功,返回状态正常”。 OperationOutcome:总体结果:FAILURE(1 个错误和 0 个警告)[错误](无详细信息)(进一步诊断:端点返回了 contentType 为“text/plain”的正文,而预期为有效的 FHIR xml/json 正文类型。这是FHIR 端点?)。'

标签: c# hl7-fhir


【解决方案1】:

我想通了。

我的第一个问题是(正如@Adriano Repetti 所指出的)客户端不知道它正在接收哪个对象,这是通过通过 FhirJsonSerializer 提供的 SerializeToString([object]) 函数运行对象来解决的.

我的下一个问题是我返回了一个字符串,而客户端期望的是 'application/json'。这已通过将“服务器”的返回类型更改为 JsonResult 并使用 JObject.Parse([string]) 将生成的字符串解析为 json 来解决。

代码

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Hl7.Fhir.Model;
using Hl7.Fhir.Rest;
using Hl7.Fhir.Serialization;
using Newtonsoft.Json.Linq;

namespace FIHR.Controllers
{
    [Route("")]
    public class HomeController : Controller
    {
        public string Index()
        {
            return "Welkom op de FIHR API";
        }

        [Route("Patient/rvrbk")]
        public IActionResult ServeClient()
        {
            Patient patient = new Patient();

            patient.Id = "1";
            patient.Gender = AdministrativeGender.Male;
            patient.Name = new List<HumanName> { new HumanName { Family = "Verbeek", Given = new List<string> { "Rik" }, Suffix = new List<string> { "The Small" } } };
            patient.BirthDate = "2004-03-10";
            patient.Active = true;

            FhirJsonSerializer serializer = new FhirJsonSerializer();

            string jstring = serializer.SerializeToString(patient);

            return Content(jstring, "application/json");
        }

        [Route("get")]
        public Patient GetClient()
        {
            FhirClient client = new FhirClient("http://localhost:54240/"); // http://vonk.fire.ly

            Patient patient = client.Read<Patient>("Patient/rvrbk"); // Patient/example

            return patient;
        }
    }
}

【讨论】:

  • 不,绝对不需要序列化然后解析然后再次序列化只是为了设置Content-Type 标头。查看最新comment
  • 我不能将'jstring'直接放在 Json() return 语句中,因为它抱怨结果只是文本,我不能将 Patient 对象直接传递给 JObject.parse 方法,因为它期望一个字符串。还是我理解错了?
  • 查看问题下方的评论。 return Content(jstring, "application/json")(并将函数原型更改为public IActionResult ServeClient()。但是(我知道我在重复......)不要在实验之外这样做。在每种方法中重复此代码不是要走的路,您可以指示ASP.NET MVC(如果您正在使用)使用您想要的 JSON 序列化程序(FhirJsonSerializer 或适配器对象,如果其接口不适合),您将拥有第一个示例的干净代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多