【发布时间】: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 端点?)。'