Providing JSON output from xPages application ... (avoiding GSON library)

David Marko  28 January 2013 09:09:46
Building the simple API for your xPages application can be very easy on Domino server if know how to do it :-). This little overview can help to serve data in JSON format from your xPages application. JSON is very easy way how to provide data to your web application. We are using it extensively for our hybrid mobile  applications built using Trigger.IO toolkit. We just have a native(hybrid) mobile (IOS and Android) application and we need Domino server to provide data we download and store into mobile device for offline availability (we use jQuery $.json here). To get this up and running the following steps are required:

1. Step ONE - create xPage as an entry point to your little API

a) create a new xpage and mark it as rendered = 'false' means xpage is not going to be rendered in standard way with HTML markup. We need our xpage to provide JSON data with proper headers ...
b) in afterRenderResponse page event we put the following code (comments available inline)  

Image:Providing JSON output from xPages application ... (avoiding GSON library)

2. Step TWO - provide JAVA code that gather your data and transform them into JSON format

Formerly we were using GSON (Google)  library to provide JAVA objects to JSON transformation. Even though its great and very popular library, in Domino environment it requires adjusting JAVA permissions for Domino server (otherwise you are getting security related errors) which requires to update XML file on file system in Notes installation folder. And this could be a real problem when you don't have access to customers server operating system or you have to maintain application running on many servers in different locations. The later was our case (app running on 9 servers in different locations - not easy to maintain such edits on file system and you still have to keep it in mind to avoid breaking the application when you reinstall the server  after some time ... ) Fortunately Domino provides similar functionality to GSON so we can use in-house solution for our purposes without adjusting the permissions ..

The following code is just a snippet of  JAVA API method that returns JSON data. The key of the concept is that we create JAVA structure representation of our database data and then transform this structure into JSON using the Domino tools ...

... comments inline ...

Image:Providing JSON output from xPages application ... (avoiding GSON library)

... hopefully someone find it usefull ...