javaspring-bootjacksonhl7-fhirhapi-fhir

Accepting FHIR Resource 'Patient' as a RequestBody in spring boot


I want to accept the JSON body of Patient FHIR resource as @RequestBody in Spring boot API. I tried to do this:

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

Using the Patient model from HAPI FHIR(https://hapifhir.io/hapi-fhir/apidocs/hapi-fhir-structures-r4/org/hl7/fhir/r4/model/Patient.html)

And called the above endpoint using postman with below request body:

{
    "resourceType":"Patient",
    "name": [{
        "use": "official",
        "given": ["temp"],
        "family": "temp"
    }],
    "birthDate": "1996-04-07"
}

but its giving below Jackson deserialization error:

[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]

Thanks in advance.


Solution

  • SpringBoot does not natively understand the FHIR objects. Any time you will try to accept FHIR in the RequestBody Jackson will try to deserialize the FHIR object and throws the given error.

    Solutions:

    1. Send the FHIR object as a raw object(String) and deserialize the same using HAPI FHIR to deserialize the string.

    @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. Creating the customer serializer & deserializer using HAPI FHIR overriding the Jackson

    Custom Deserilizer

    Custom Serilizer

    Registering custom Serializer and Deserializer