[Jaspersoft] Creating custom Spring managed DataSource

Problem Statement

Often for reporting, we need to gather data from many diverse resources. In case we are using Jaspersoft server, its sometimes not possible to manage with the existing DataSources provided by Jaspersoft. In this article, we will explore step by step how to write our custom DataSource. The main challenge is that in the given examples like the WebScraperDataSource, the factory beans are not Spring managed. They are instantiated by the framework through a no-argument constructor by reflection. In case I need to talk to other Spring managed beans, it becomes incredibly difficult. In the below example, I will try to put a hack so that we can use our own Spring managed DataSourceFactory.

Dev Environment Setup

I would need to hook into the Jaspersoft’s framework and for that, I need to create my own jar which can then be deployed into the WEB-INF/lib directory of Jaspersoft’s webapp. I am using Maven. The following dependencies need to be declared:

Note that some of these are not available in Maven public repos and need to manually deployed from the webapps/WEB-INF/lib directory:

Spring bean configuration

Jaspersoft works with Spring, thanks God for that!

jasper-bean-creation-sequence-1

jasper-bean-creation-sequence-2

Finally, this is what the Spring file looks like:

Information flow

jasper-flow-highlevel-1

 

jasper-flow-highlevel-2

Making our custom Query appear on the Jaspersoft

We need to make further configuration changes before our custom query appears on the Jaspersoft screen.

Make an entry in the applicationContext-rest-services.xml

Register your factory at WEB-INF/classes/jasperreports.properties

Make the following entry:

net.sf.jasperreports.query.executer.factory.SpringBeanQuery=com.swayam.demo.jasper.SpringBeanQueryExecutorFactory

Generate report

We are all set now to generate a report. Create the DataSource we have just now defined. Then create a JRXML and define the query there:

Link the DataSource and the JRXML. You need to define an Input as well. Then run the report. You should see the below report:

generated-reportResources

The sources for this can be found here: https://github.com/paawak/blog/tree/master/code/jasper/custom-datasource

 

4 thoughts on “[Jaspersoft] Creating custom Spring managed DataSource

  1. This is great work. I am trying to follow the steps given above. When I run the jasper report, it asks to pick the datasource. I have configured bean data source as : customDataSource bean_name as BEAN_NAME and Bean Method as METHOD_NAME. It throws an exception that bean_name is not defined and invalid.

    If I do not use the data source, then it throws a null pointer exception.

      1. Thanks. The custom spring xml file exists in tomcat web-inf directory. We could see the SpringBeanDataSource being listed as part of datasource selection in Jasper. We have given Bean name as the class name/bean ID defined in Custom Spring XML and method name which is available under Java Class. Method Name is : createService. But, when executing the jasper report, it is giving Method Not found exceptions. Could you please let us know what we are doing wrong here.

  2. It works. now I am able to get the beam name and method names defined.. and able to associate the data source. The beam name and method names could be the new class / methods created to invoke the dummy service created and included as part of custom data spring xml. Now, we integrated the datasource with jrxml.. there are further steps to define input, etc. which are missing.

Leave a Reply

Your email address will not be published. Required fields are marked *