Reputation: 675
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.
Upvotes: 2
Views: 3028
Reputation: 432
The solution of @Andrew Hall worked for me. I had to vary it because instead of a controller, I was using sending a FHIR R4 Patient resource object to from my proprietary app code to a FHIR server using RestTemplate.
I did what Andrew Hall's solution points to except that instead of registering the HapiMessageConverter to a MvcConfigurer, I had to register them to a Rest Template. Fleshing out the code here for others who come across this variation.
@Bean(name = "intAppFhirRestClient")
public RestTemplate fhirRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(0,new HapiHttpMessageConverter(FhirContext.forR4()));
return restTemplate;
}
Also, registering the converter will only work if you have FhirContext and R4 in your proprietary code dependencies.
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-structures-r4</artifactId>
<version>5.5.7</version>
</dependency>
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
<artifactId>hapi-fhir-base</artifactId>
<version>5.5.7</version>
</dependency>
Upvotes: 1
Reputation: 183
A slight variation on the second solution. You can create a custom Spring MessageConverter to serialize/deserialize FHIR. To do that:
Upvotes: 2
Reputation: 26
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:
@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();
}
}
Registering custom Serializer and Deserializer
Upvotes: 1