在 Spring Boot 中接受 FHIR 资源“Patient”作为 RequestBody

问题描述 投票:0回答:2

我想在 Spring boot 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-structs-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]

提前致谢。

java spring-boot jackson hl7-fhir hapi-fhir
2个回答
1
投票

SpringBoot 本身并不理解 FHIR 对象。每当您尝试在 RequestBody 中接受 FHIR 时,Jackson 都会尝试反序列化 FHIR 对象并抛出给定的错误。

解决方案:

  1. 将 FHIR 对象作为原始对象(字符串)发送,并使用 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. 使用 HAPI FHIR 覆盖 Jackson 创建客户序列化器和解串器

Custom Deserilizer

Custom Serilizer

Registering custom Serializer and Deserializer


0
投票

第二个解决方案略有不同。您可以创建自定义 Spring MessageConverter 来序列化/反序列化 FHIR。为此:

  1. 创建 HapiHttpMessageConverter 来序列化/反序列化 FHIR 资源
  2. 创建 HapiMessageConverterConfigurer 来注册转换器
  3. 向应用程序上下文注册 HapiMessgeConverterConfigurer
© www.soinside.com 2019 - 2024. All rights reserved.