Search code examples
javahl7-fhirhapi

How to implement Search operation for an Observation Resource Provider for Hapi Fhir server?


I am trying to understand how the RESTful Server in Hapi Fhir works and I wanted to implement some @Search methods for Observation resources.

Currently, I have this @Read operation, which successfully works when trying to access the resource (like this: http://localhost:8080/NewFHIRServer/fhir) from the browser:

@Read()
public Observation readObservationById(@IdParam IdDt theId) {
    for (Entry<Long, Deque<Observation>> entry : myPatientIdToObservations.entrySet())
    {
        for (Observation obs : entry.getValue()) {
            if (obs.getId().equals(theId)) {
                return obs;
            }
        }
    }

    throw new ResourceNotFoundException(theId);
}    

However, when I try to do something similar for the @Search operation, I am getting errors. I would like to be able to get the response by running the search like this (or similar):

Bundle response = client
        .search()
        .forResource(Observation.class)
        .where(Observation.SUBJECT.hasId("Patient/1"))
        .execute();   

What parameters do I need to have in my @Read method in order to make this possible? The error I am getting right now is the following:

The FHIR endpoint on this server does not know how to handle GET operation[Observation] with parameters [[subject]]

and it is obvious why it doesn't work, because my header looks like this:

public Observation searchObservationById(@IdParam IdDt theId)     

I have been looking at examples to try to figure this out and I don't quite understand what the syntax in this parameter means:

public List<Patient> getPatient(@RequiredParam(name = Patient.SP_FAMILY) StringParam theFamilyName)...

How would you make the query in order to use this last example?

Thank you


Solution

  • To implement a search method, you need to use @Search instead of @Read on the method. You then use zero-or-more parameters annotated with @OptionalParam or @RequiredParam.

    To make your specific example work, you need a search method which implements the _id search parameter, e.g.

    @Search public List<Patient> getPatient(@RequiredParam(name = Patient.SP_RES_ID) StringParam theId) { }