[英]Accepting FHIR Resource 'Patient' as a RequestBody in spring boot
我想在 Spring 引導 API 中接受 Patient FHIR 資源的 JSON 主體作為 @RequestBody。我嘗試這樣做:
@RestController
@RequestMapping("/api")
public class DemoController {
@PostMapping("/Patient/save")
public String savePatientDetails(@RequestBody Patient p) {
IGenericClient client = fhirContext.newRestfulGenericClient("http://localhost:8080/fhir");
MethodOutcome s = client.create().resource(p).prettyPrint()
.encodedJson()
.execute();
return s.toString();
}
}
使用來自 HAPI FHIR 的患者 model( https://hapifhir.io/hapi-fhir/apidocs/hapi-fhir-structures-r4/org/hl7/fhir/r4/model/Patient.html )
並使用帶有以下請求正文的 postman 調用上述端點:
{
"resourceType":"Patient",
"name": [{
"use": "official",
"given": ["temp"],
"family": "temp"
}],
"birthDate": "1996-04-07"
}
但它給出以下 Jackson 反序列化錯誤:
[nio-8081-exec-1] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class org.hl7.fhir.r4.model.Patient]]: com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "referenceElement": org.hl7.fhir.r4.model.Reference#setReferenceElement(1 params) vs org.hl7.fhir.r4.model.Reference#setReferenceElement(1 params)
2022-02-25 09:32:43.332 WARN 71185 --- [nio-8081-exec-1] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class org.hl7.fhir.r4.model.Patient]]: com.fasterxml.jackson.databind.JsonMappingException: Conflicting setter definitions for property "referenceElement": org.hl7.fhir.r4.model.Reference#setReferenceElement(1 params) vs org.hl7.fhir.r4.model.Reference#setReferenceElement(1 params)
2022-02-25 09:32:43.356 WARN 71185 --- [nio-8081-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported]
提前致謝。
SpringBoot 本身並不理解 FHIR 對象。 任何時候您嘗試在 RequestBody Jackson 中接受 FHIR 時,都會嘗試反序列化 FHIR object 並拋出給定的錯誤。
解決方案:
@RestController @RequestMapping("/api") public class DemoController { @PostMapping("/Patient/save") public String savePatientDetails(@RequestBody String p) { IGenericClient client = fhirContext.newRestfulGenericClient("http://localhost:8080/fhir"); IParser parser = fhirContext.newJsonParser(); Patient firObject=parser.parseResource(Patient.class,p); MethodOutcome s = client.create().resource(firObject).prettyPrint().encodedJson().execute(); return s.toString(); } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.