How to develop custom module and rest api

Application Name: Platform
Version Number: 2.4.3 Build 0ff0ed

Question: I’m new to Openmrs. I’m trying create my own module. I already looked these articles
Creating Your First Module - Documentation - OpenMRS Wiki
Creating Modules - Documentation - OpenMRS Wiki
Making Your First Module · GitBook

With these articles i already developed basic module. now i need to know how to connect this module to database and retrieve data? please help me? thanks in advance.

Dear @abayomi Here is the link that will guide you on the general overview about the rest module Adding a Web Service Step by Step Guide for Core Developers - Documentation - OpenMRS Wiki

Thanks for the reply. I’m receiving error.

it’s showing java.sql.SQLSyntaxErrorException: Unknown column ‘this_.changed_by’ in ‘field list’ but i cannot find the place.

my code: - package org.openmrs.module.basicexample.web.controller;import org.springfram - Pastebin.com

please help me to fix this.

Your Item class its supposed to inherit all attributes from BaseOpenmrsData in the liquibase file. since your extending BaseOpenmrsData

Thanks for the reply. how can i save record. I tried post request to http://localhost:8080/openmrs/ws/rest/v1/item but it showing me

Request processing failed; nested exception is org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException

It means your missing out the save method in the controller that does post payload from the endpoint to the database: Take a look at this for-example openmrs-module-webservices.rest/DrugResource1_8.java at fa0e04d6cde6e3a7f55a672217b2419257692ee6 · openmrs/openmrs-module-webservices.rest · GitHub

Do you mind sharing the link to the repository so that I could have a look at where your going wrong.

Thanks for the reply. here is the code.

Try autowiring the service in the ItemResource class

@Autowired
BasicexampleService basicexampleService;

then on save method try out this

Item item = basicexampleService.saveItem(t);

I change it but still getting the same error

Please share the github repository link here

Thanks for the reply. here is the repository

Can you help me to solve this plz?

Dear @abayomi please check on the repo issues, there are comments which I have created.

Regards,

1 Like

Thank you very much. i will check that.

Thank you very much for the help. i did the modification as you mentioned. but still not working. getting error

Request processing failed; nested exception is org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException

i updated github code repo. can you check what’s wrong???

Please check again, everything works now if you replace the with what i have changed but remember I did make changes on setOwner using User currentUser = Context.getAuthenticatedUser(); to set the owner value you will be required to update the tests so as to pass.

after i replace these there is an error

There are test failures.[ERROR] [ERROR] Please refer to D:\rest\api\target\surefire-reports for the individual test results.

in that file there is an error

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.555 sec <<< FAILURE! saveItem_shouldSetOwnerIfNotSet(org.openmrs.module.basicexample.api.BasicexampleServiceTest) Time elapsed: 0.48 sec <<< ERROR! org.openmrs.api.APIException: A user context must first be passed to setUserContext()...use Context.openSession() (and closeSession() to prevent memory leaks!) before using the API

so i keep the old code as it is. but i changed other modifications. now i’m receiving Request processing failed; nested exception is java.lang.NullPointerException

I’m stuck here. can you help me

Hello, @abayomi check on the github and merge the PR

1 Like