2

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

randombee
  • 699
  • 1
  • 5
  • 26

1 Answers1

2

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) { }

James Agnew
  • 701
  • 4
  • 3
  • I have been able to run searches for patients by looking at your github documentation, but I am having trouble doing the same for Observations, since I don't know what to put in the parameter of the .where(...) to get the bundle when I query and I don't know what to put in the header of my @Search function (for example, to query all observations with a given patient id). Is there any kind of documentation that I can use to find out about this? – randombee Mar 09 '16 at 12:06
  • To search for instances of Resource "A" where you want to find matches on a reference to resource "B" (e.g. find all Observations with a given subject) you need to use ReferenceParam. E.g. @OptionalParam(name="subject" ) ReferenceParam theSubject – James Agnew Mar 10 '16 at 13:26