AEM Developer Series
- Day 00: AEM Developer Series
- Day 01: Introduction to AEM
- Day 02: AEM Architecture
- Day 03: Setting up AEM Development Environment
- Day 04: Developing First OSGi Bundle
- Day 05: Working with Sling Servlets in AEM
- Day 06: Playing with Sling Post Servlet
- Day 07: Creating your first component in AEM
- Day 08: Dueling with JavaScript Use API
- Day 09: Dueling with Java User API
- Day 10: Getting to know Sling Models
- Day 11: Client Libraries in Action
- Day 12: Creating your custom OSGi Configuration
- Day 13: Schedulers in AEM
- Day 14: Eventing in AEM
- Day 15: Custom Workflows in AEM
- Day 16: Creating JMX Beans in AEM
- Day 17: Working with QueryBuilder API
- Day 18: Working with Granite Datasources in AEM
- Day 19: Replication API in Action
- Day 20: Working with Users and Groups in AEM
You can find all the Out of the box OSGi configurations at - http://<host>:<port>/system/console/configMgr. Apart from out of the box configurations, we can also create our custom configurations. In this post, we will be creating a custom out of the box configuration which reads the user input and gets the JSON reponse from a web service.
Code in Action
- To make a custom OSGi configuration, we need to first create an interface whose public methods will represent the fields in the configuration.
- Create an interface named HttpConfiguration and paste the following code in it.
- This configuration has a checkbox Enable Config, a drop-down Protocol, two text fields Server and Endpoint.
- Now create an interface HttpService to make the http call as below
- Create an implementation class HttpServiceImpl as below.
- This is an OSGi component in which we are reading values from the OSGi configuration. Notice that we are using @Designate annotation to link this class to the configuration.
- Now create a simple Sling Servlet to use this component as follows.
- Go to the ./system/console/configMgr and search for Http Configuration and open it and configure it accordingly. Then save.
![]() |
OSGi Configuration |
- Now hit the request - http://<host>:<port>/bin/demo/httpcall and you will get the JSON response as follows
[{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}, {"userId": 1, "id": 2, "title": "quis ut nam facilis et officia qui", "completed": false}, {"userId": 1, "id": 3, "title": "fugiat veniam minus", "completed": false}, {"userId": 1, "id": 4, "title": "et porro tempora", "completed": true}, ...]
Conclusion
Congratulations!! 🙋 today we created a custom OSGi configuration that reads values from the user and provides the results accordingly. I hope you enjoyed this post.
You can find the complete code of this project on my GitHub in this commit. Feel free to fork or open issues, if any.
I would love to hear your thoughts on this and would like have suggestions from you to make it better.
Happy Coding 😊
Nice Article Bro !!
ReplyDeleteThanks for sharing good knowledge. You are really doing great work :)
This comment has been removed by the author.
ReplyDeleteThis is one of the only guides to setting up an OSGI service for AEM that I can find, so its been a life saver. The only problem we have is when we try to create a cfg.json file to deploy the settings, it doesnt work (we end up with two duplicate entries in configMgr, the one you get above, plus the one created from the config file, both with the same PID. The service is reading the wrong one, but there seems nothing we can do about it.
ReplyDeletei got a error in line number 66 at "String response = Network.readJson(url);" in HttpServiceImpl program.how can i resolve that issue? and how can i get this Network related import statement?
ReplyDeletei have got some errors....how can i contact you?
ReplyDelete