Infinite Recursion

1,349 views
Skip to first unread message

Jean-Karlo Accetta

unread,
Jul 24, 2018, 2:16:30 PM7/24/18
to HAPI FHIR
Hi,


I'm using Spring Boot to return some results from a HAPI FHIR backend. I'm getting an infinite recursion error when trying to return a Bundle from a generic rest client. Here's the code snippet:

@GetMapping(value = {"/condition/", "/condition"})
public Bundle getAll() {
 
Bundle bundle = clinacuityProperties.getFhirContext().newRestfulGenericClient(url)
 
.search()
 
.forResource(Condition.class)
 
.returnBundle(Bundle.class)
 
.execute();

 
return bundle;

}

Here's the error that I'm running into:

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: org.hl7.fhir.dstu3.model.StringType["idElement"]->org.hl7.fhir.dstu3.model.StringType["idElement"]->org.hl7.fhir.dstu3.model.StringType["idElement"]-> [. . . repeats several hundred times . . .] ->org.hl7.fhir.dstu3.model.StringType["idElement"]->org.hl7.fhir.dstu3.model.StringType["formatCommentsPre"])

(There's a full stack trace attached)

I don't know what could possibly be wrong with this, and I've tried adding each entry to a List<Condition> and returning that instead, but it still crashes. It works fine if I go to the same {url}/Condition and get all the conditions (they're returned normally), but the client is failing with that weird error. Do I need to use some parser or add some setting to exclude recursive results? The Conditions on my test server are very simple objects with minimal data in them.

Thanks!
  - JK Accetta

error.txt

Jean-Karlo Accetta

unread,
Jul 26, 2018, 10:03:36 AM7/26/18
to HAPI FHIR
I figured it out after a while, though I don't know if this is the best way? Anyway, posting in case anyone else is trying to return the results from their own endpoints:

As mentioned on the first post, I'm using Spring's @RestController and @GetMapping annotations, but I was returning the raw bundle; what fixed it for me was to encode the bundle to a String using the IParser methods:


@GetMapping(value = {"/condition/", "/condition"}, produces = "application/json")
public Object getAll() {
   
Bundle bundle = properties.getFhirContext().newRestfulGenericClient(url)
       
.search()
       
.forResource(Condition.class)
       
.returnBundle(Bundle.class)
       
.execute()
 
   
return properties.getFhirContext().newJsonParser().encodeResourceToString(bundle);
}


Best regards,
   - JK

bell....@gmail.com

unread,
Nov 27, 2018, 7:06:36 PM11/27/18
to HAPI FHIR
Thank you very much. I had the same issue and this solved it for me!
Reply all
Reply to author
Forward
0 new messages