英文:
Returning Hapi FHIR element in restful server
问题
以下是您提供的代码的翻译部分:
@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 数据是递归的且无限循环的。响应的片段如下:
{
"formatCommentsPre": [],
"formatCommentsPost": [],
"id": "1",
"meta": {
"formatCommentsPre": [],
"formatCommentsPost": [],
"id": null,
"extension": [],
"versionId": null,
"lastUpdated": null,
"profile": [],
"security": [],
"tag": [],
"empty": true,
"tagFirstRep": {
"formatCommentsPre": [],
...
}
}
}
有没有人知道我做错了什么,以及发送 FHIR 患者响应的正确方式是什么?
英文:
I have a basic springboot application set up that'll return string values on get calls, but when I try to return a mock Patient object I get a weird error
My code:
@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;
}
The Error seems to be that the created json is recursive and never ending. A snippet of the response is:
{"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":
Does anybody have an idea as to what I'm doing wrong and what the correct way to send a FHIR response for a patient is?
答案1
得分: 1
你应该使用 FHIR 解析器将你的对象转换为 JSON,然后将 JSON 值作为简单字符串返回。
// 创建一个 FHIR 上下文
FhirContext ctx = FhirContext.forR4();
// 创建要序列化的患者资源
Patient patient = new Patient();
patient.addName().setFamily("Simpson").addGiven("James");
// 实例化一个新的 JSON 解析器
IParser parser = ctx.newJsonParser();
// 进行序列化
String serialized = parser.encodeResourceToString(patient);
// 返回结果
return new ResponseEntity<>(serialized, HttpStatus.OK);
英文:
You should use FHIR Parser to cast your object into JSON and then return the JSON value as a simple string.
// 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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论