在 restful 服务器中返回 Hapi FHIR 元素
Posted
技术标签:
【中文标题】在 restful 服务器中返回 Hapi FHIR 元素【英文标题】:Returning Hapi FHIR element in restful server 【发布时间】:2020-12-29 09:09:05 【问题描述】:我设置了一个基本的 springboot 应用程序,它将在 get 调用时返回字符串值,但是当我尝试返回一个模拟 Patient 对象时,我得到一个奇怪的错误
我的代码:
@RequestMapping("/test2")
Patient test2()
Patient patient = new Patient();
patient.setId("1");
patient.addName().setFamily("Bar").addGiven("Foo").addGiven("M");
patient.addAddress().addLine("Address Line 1");
patient.addAddress().setCity("City Name");
patient.addAddress().setCountry("Country Name");
patient.addTelecom().setValue("555-555-1111");
return patient;
错误似乎是创建的json是递归的并且永无止境。响应的 sn-p 是:
"formatCommentsPre":[],"formatCommentsPost":[],"id":"1","meta":"formatCommentsPre":[],"formatCommentsPost":[],"id":null,"extension":[],"versionId":null,"lastUpdated":null,"profile":[],"security":[],"tag":[],"empty":true,"tagFirstRep":"formatCommentsPre":[],"formatCommentsPost":[],"id":null,"extension":[],"system":null,"version":null,"code":null,"display":null,"userSelected":false,"empty":true,"displayElement":"formatCommentsPre":[],"formatCommentsPost":[],"id":null,"extension":[],"valueNotNull":"","primitive":true,"empty":true,"value":null,"valueAsString":null,"idElement":"formatCommentsPre":[],"formatCommentsPost":[],"id":null,"extension":[],"valueNotNull":"","primitive":true,"empty":true,"value":null,"valueAsString":null,"idElement":"formatCommentsPre":[],"formatCommentsPost":[],"id":null,"extension":[],"valueNotNull":"","primitive":true,"empty":true,"value":null,"valueAsString":null,"idElement":"formatCommentsPre":[],"formatCommentsPost":[],"id":null,"extension":[],"valueNotNull":"","primitive":true,"empty":true,"value":null,"valueAsString":null,"idElement":"formatCommentsPre":[],"formatCommentsPost":[],"id":null,"extension":[],"valueNotNull":"","primitive":true,"empty":true,"value":null,"valueAsString":null,"idElement":"formatCommentsPre":[],"formatCommentsPost":[],"id":null,"extension":[],"valueNotNull":"","primitive":true,"empty":true,"value":null,"valueAsString":null,"idElement":"formatCommentsPre":[],"formatCommentsPost":[],"id":null,"extension":[],"valueNotNull":"","primitive":true,"empty":true,"value":null,"valueAsString":null,"idElement":"formatCommentsPre":[],"formatCommentsPost":[],"id":null,"extension":[],"valueNotNull":"","primitive":true,"empty":true,"value":null,"valueAsString":null,"idElement":"formatCommentsPre":[],"formatCommentsPost":[],"id":null,"extension":
是否有人知道我做错了什么以及为患者发送 FHIR 响应的正确方法是什么?
【问题讨论】:
【参考方案1】:您应该使用 FHIR Parser 将您的对象转换为 JSON,然后将 JSON 值作为简单字符串返回。
// Create a FHIR context
FhirContext ctx = FhirContext.forR4();
// Create a Patient resource to serialize
Patient patient = new Patient();
patient.addName().setFamily("Simpson").addGiven("James");
// Instantiate a new JSON parser
IParser parser = ctx.newJsonParser();
// Serialize it
String serialized = parser.encodeResourceToString(patient);
// Return the result
return new ResponseEntity<>(serialized, HttpStatus.OK);
【讨论】:
以上是关于在 restful 服务器中返回 Hapi FHIR 元素的主要内容,如果未能解决你的问题,请参考以下文章
ionic 后台Api服务, 使用rest-hapi , node.js 创建 RESTful API Service , 附完整源代码
如何在自动创建的 API Rest Hapi 之前获取 validateFunc 的结果
无法在heroku中加载favicon.ico for rest api