/
How to create a Report that pulls data from a REST API?

How to create a Report that pulls data from a REST API?


Through this guide we are going to explain how to create a Report that pulls data from a REST API to fill the Report itself at runtime.

1. Create Jasper Report template with TIBCO JasperSoft Studio


We are going to model a thermometer that will plot data retrieved from a JSON REST API of the Onesait Platform.


The JSON input (fields) will have the following format:

{
"title": "Germany June",
"value": -10,
"rangeMin": -40,
"rangeMax": 50,
"firstSubRangeMin": 0,
"firstSubRangeMax": 0,
"secondSubRangeMin": 15,
"secondSubRangeMax": 15,
"thirdSubRangeMin": 50,
"thirdSubRangeMax": 50

}



Fields are defined in the template:



Now the input JSON datasource will be our API, so we have to add the following lines to the XML:

(How to add a JSON datasource programmatically: http://jasperreports.sourceforge.net/sample.reference/jsondatasource/)

(How to add a JSON datasource visually: https://www.youtube.com/watch?v=UG1RU393FtE)



A "net.sf.jasperreports.json.source" parameter is needed in the XML in order to pull the data.


Here you have the full example: thermometer.jrxml

2. Upload the template


Now we go to the web console and upload the template:


 


Then we click on 'Create':


We fill the data, upload the .jrxml and click on 'New'.




3. Create the ontology


We have to create the ontology with the data structure as mentioned above.


We go to data import tools:



We paste the JSON into the text area:

{
"title": "Germany June",
"value": -10,
"rangeMin": -40,
"rangeMax": 50,
"firstSubRangeMin": 0,
"firstSubRangeMax": 0,
"secondSubRangeMin": 15,
"secondSubRangeMax": 15,
"thirdSubRangeMin": 50,
"thirdSubRangeMax": 50

}


With both checkboxes checked (new ontology, and Import data to RTDB), we click on 'Submit', we fill the input data, and lastly we click on 'Submit' again:


 


When created, you'll be redirected to the ontology page:



 

4. Create the API


Now that we have created the model layer, and loaded a json sample (at import time), we are ready to create our API.


We go to APIs and click on 'Create'.



We create the API selecting the ontology you've just created, and we enable operation 'GET (ALL)':



Finally, we click on 'Create'.


We keep our 'endpoint base' (http://localhost:19100/api-manager/server/api/v1/Thermometer) as well as our User API Key, because we will need them to run the report.


API Key can be found on the APIs screen, clicking on 'User tokens':






5. Run the Report


Finally, we only need to run the report, filling the parameters needed.


We go back to Reports page, find 'Thermometer' and click on 'Generate' :

 


A window will pop to fill the parameters


We make sure to check the boxes (left side 'Send parameters') in order to send the parameters to the report.

We then click on 'Generate', and the generated PDF will be downloaded.




In this case we are representing data from Polonia (Poland):







Related content