Show me the data
Often, usually, nearly-always I want my report server to give me a nicely formatted view of my data. A “report”. But not always. Sometimes I just want my data. I’ll format it myself, thank you. JasperReports Server can do that, but I had never done it before. Here are my notes from figuring it out.
Prerequisites
- JasperReports Server v5.1 or later is installed and working.
- A data source and a domain are defined.
- I have at least one query written to get started with.
- (If you just want to follow along, you can use the sample domain included with JRS and the sample query provided below.)
- A REST Client for testing. Use whatever you like. I recommend Advanced REST Client (a Chrome plugin).
Construct the URL
Here is the basic structure of the URL we need to create:
{scheme}://{host}:{port}/{contextPath}/rest_v2/queryExecutor/{resourceUri}?q={queryString}
In my case I’ll use the sample domain “Supermart Domain”. The URI path is “/public/Samples/Domains/supermartDomain”
I constructed a simple query using the Domain Expression Language (domEL) syntax. Refer to the JasperReports Server User Guide for details. Here’s the query in readable form:
<query> <queryFields> <queryField id="inv_store.inv_store__store_name" /> <queryField id="inv_store.inv_store__store_type" /> <queryField id="inv_store__store_contact.inv_store__store_contact__store_manager" /> </queryFields> <queryFilterString>inv_store__region.inv_store__region__sales_state_province == 'CA'</queryFilterString> </query>
Here’s the same query with spaces and carriage returns removed and url encoded:
%3Cquery%3E%3CqueryFields%3E%3CqueryField+id%3D%22inv_store.inv_store__store_name%22+%2F%3E%3CqueryField+id%3D%22inv_store.inv_store__store_type%22+%2F%3E%3CqueryField+id%3D%22inv_store__store_contact.inv_store__store_contact__store_manager%22+%2F%3E%3C%2FqueryFields%3E%3CqueryFilterString%3Einv_store__region.inv_store__region__sales_state_province+%3D%3D+'CA'%3C%2FqueryFilterString%3E%3C%2Fquery%3E
With the domain and the query identified, now I have everything I need to construct the url:
http://localhost/jasperserver-pro/rest_v2/queryExecutor/public/Samples/Domains/supermartDomain?q=%3Cquery%3E%3CqueryFields%3E%3CqueryField+id%3D%22inv_store.inv_store__store_name%22+%2F%3E%3CqueryField+id%3D%22inv_store.inv_store__store_type%22+%2F%3E%3CqueryField+id%3D%22inv_store__store_contact.inv_store__store_contact__store_manager%22+%2F%3E%3C%2FqueryFields%3E%3CqueryFilterString%3Einv_store__region.inv_store__region__sales_state_province+%3D%3D+'CA'%3C%2FqueryFilterString%3E%3C%2Fquery%3E
I’m ignoring authentication. For a quick test you can first login to JRS, then paste in this url. You’ll get a response in the default format: XML. Refer to the JasperReports Server Web Service Guide for details on how to securely authenticate programmatically.
Show me the data
But I want JSON rather than XML. No problem; I just add the header “Accept: application/json” to my request. Now my result set comes back in JSON.
Why is this cool? Now I have complete flexibility to render the data as I like, but I still get the benefits of the report server:
- Security: I’ll only see the data that I should see)
- Scalability: JRS does intelligent data caching
- Consistency: I’ll continue to use the same data source for most of my data visualizations which get rendered as reports on the server.