在 spring 引导中接受 FHIR 资源 'Patient' 作为 RequestBody

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(https://hapifhir.io/hapi-fhir/apidocs/hapi-fhir-structures-r4/org/hl7/fhir/r4/model/Patient.html)

的患者模型

并使用具有以下请求正文的邮递员调用上述端点:

{
    "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 中接受 FHIR,Jackson 都会尝试反序列化 FHIR 对象并抛出给定的错误。

解决方案:

  1. 将 FHIR 对象作为原始对象 (String) 发送,并使用 HAPI FHIR 反序列化该字符串。

@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();
    }
}

  1. 使用覆盖 Jackson 的 HAPI FHIR 创建客户序列化器和反序列化器

Custom Deserilizer

Custom Serilizer

Registering custom Serializer and Deserializer