CommunicationProvider return body and Test - R4 structure

68 views
Skip to first unread message

Pablo

unread,
Dec 21, 2022, 5:43:45 AM12/21/22
to HAPI FHIR
Hi All.
We are building a new project with Hapi FHIR and spring boot.

We have a CommunicationProvider that implements IResourceProvider. The create method has a ResourceParam Communication. We do some logic calling to a service, and we want to return the communication created and a 201 status.
How can we return the communication object? 

Building the IT test of this provider we have another question related with this, we have tested several options but always we get a null body.

Take a look to the code:
a) CommunicationProvider
CommunicationProviderjava.jpgCommunicationProviderITjava.JPG
b) CommunicationIT
CommunicationProviderITjava.JPG

How a create method can return something in the body ? 
Thanks in advance


James Agnew

unread,
Dec 21, 2022, 8:47:49 AM12/21/22
to Pablo, HAPI FHIR
Hi Pablo,

You should be able to call MethodOutcome#setResource() to supply a body. You can also supply an OperationOutcome via MethodOutcome#setOperationOutcome() if you want to allow the client to negotiate what they want in the return payload using the Prefer Header.

Cheers,
James

--
You received this message because you are subscribed to the Google Groups "HAPI FHIR" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hapi-fhir+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/hapi-fhir/6796fdf3-1a0c-4677-97d1-5bd30ab9cc38n%40googlegroups.com.

Pablo

unread,
Dec 21, 2022, 9:56:34 AM12/21/22
to James Agnew, HAPI FHIR
Thanks James!
I have tried to add methodOutcome.setResource(communication)
MethodOutcome methodOutcome = new MethodOutcome();
methodOutcome.setId(new IdType(ResourceType.Communication.name(), communication.getId(), "1"));
methodOutcome.setResource(communication);
methodOutcome.setCreated(true);
log.info(" return create methodOutcome {}", methodOutcome);
return methodOutcome;
But I have found this exception:

image.png

A couple of more questions, we are trying to test all our providers with Integration test. We have some issues with jackson. Do you have any examples of test providers? we have endpoints Create, Put, Post ...

* GET method: running fine, but we are parsing to strings, no to entity.
ResponseEntity<String> entity = this.restTemplate.getForEntity("/fhir/Device/" + deviceId, String.class);
Assertions.assertEquals(entity.getStatusCode(), HttpStatus.OK);
* Post method: with restTemplate.postForEntity looks fine.
String jsonCommunication = Util.getFileContent("communication.json");

HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.CONTENT_TYPE, "application/fhir+json");
headers.add(HttpHeaders.ACCEPT, "application/fhir+json");
HttpEntity<String> entity = new HttpEntity<>(jsonCommunication, headers);
ResponseEntity<MethodOutcome> responseEntity = restTemplate.postForEntity("/fhir/Communication", entity, MethodOutcome.class);

Assertions.assertEquals(responseEntity.getStatusCode(), HttpStatus.CREATED);
* PUT:
we don't know how to test this type of endpoints due to the need to add the headers with the fhir+json content_type. Do you know how to test these types of endpoints? or do you know where we can find examples?

* PATCH
we don't know how to test this type of endpoints due to the need to add the headers with the fhir+json content_type. Do you know how to test these types of endpoints? or do you know where we can find examples?

Thanks in advance

James Agnew

unread,
Dec 21, 2022, 10:19:51 AM12/21/22
to Pablo, HAPI FHIR
That error is client side, not server side.

I'd recommend using the HAPI FHIR Generic client as your client library to ensure that your client is properly implementing the FHIR Spec. Spring's RestTemplate is a great piece of infrastructure and I've used it plenty of times for other purposes, but it's tricky to get it to properly parse FHIR payloads which is the issue you're seeing here. It's doable, but you generally need to have it just return a String and manually handle parsing.

Cheers,
James
Reply all
Reply to author
Forward
0 new messages