Adding REST API(*Controller.java)

Bahmni (and OpenMRS) REST API swagger can be accessed on a running Bahmni instance on this url:

https://<bahmni-ip>:<port>/openmrs/module/webservices/rest/apiDocs.htm

For instance on the Bahmni Demo server, the URL will be: REST API (Demo Server)


In Spring, REST endpoint are just Spring MVC controllers. If you want to expose part of entity as a rest resource you can use @RestController annotation or @Controller annotation.

Let’s take appointments entity as an example. For this appointments entity, We will have basic operations like getting all the appointments information, creating an appointment, changing the existing appointment and few more. Once we come up with the list of operations, we need to distinguish them between GET and POST calls (This wiki page covers mainly GET and POST API calls).

  • Getting all appointments Information is a GET call
  • Creating an Appointment / changing the existing appointment is a POST call
  • To void/delete existing appointment service is a DELETE call

Once all the analysis is done, We need to create a Spring class with @Controller annotation as mentioned below. With @Controller annotation the class acts as a REST Controller class. The class-level @RequestMapping annotation maps "/appointment" path onto AppointmentController class.

1. How to Implement a GET call

Once the controller class is ready, we need to implement a method that gets all the appointment details.  Below is the sample method signature for the GET call.

The above method-level @RequestMapping annotation maps "all" path onto getAllAppointments method. To know more about @RequestParam,  check this Spring REST documentation.

2. How to Implement a POST Call

We need to implement a method that creates or changes an appointment. Below is the sample method signature for the POST call.

The above @RequestMapping annotation maps "/{appointmentUuid}/changeStatus" path in the URL onto  transitionAppointment method. To know more about @PathVariables and @RequestBody check this Spring REST documentation.

Please check AppointmentController class for  GET and POST calls.

On this Page

The Bahmni documentation is licensed under Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)