I have developed a template design in JasperSoft Studio and upload jrxml
file to JasperServer. I want to send data (JSON or XML) to filling report template from my python application and take back report in some popular formats like PDF, XLS using REST API. I do not want to store the data on the server. How can I do this? Or data must be stored on the server and there is no alternative way of their transmission by WEB-Service?

- 100
- 2
- 10
-
Which data are you referring to? The JRXML sources or the generated reports? – tobi6 May 24 '16 at 09:08
-
The data for filling the report template. I mean JRXML data sources. – Sergey Sigaev May 24 '16 at 09:11
-
Just out of curiosity: Why wouldn't you want to store the report data on the report server? Do you have some other kind of repository? – tobi6 May 24 '16 at 09:23
-
This is due to the fact that different users would like to receive the report, but with different data, therefore I must transmit data in the request to the server. Is that possible? – Sergey Sigaev May 24 '16 at 09:38
-
This is the main use case of parameters for reports. See my answer for details. – tobi6 May 24 '16 at 09:46
-
The parameters needed to customize the report. I understand how to use them in a report in sql query or specifications for other features of the report like report title. I don't understand how using parameters solve my problem. – Sergey Sigaev May 24 '16 at 10:01
2 Answers
The data does not necessarily have to reside on the server.
You could design your template in such way that you can pass the data via input control parameters as @tobi6 suggested. Then you could use either the reports service or the reportExecutions service to get the desired output.
In your case, the data could be the actual data(XML or JSON) or the source of the data(a URL to the data file).
Here are some basic samples for working with XML data(for JSON is quite similar):
With actual data as parameter
The JasperReports template:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.1.1.final using JasperReports Library version 6.1.1 -->
<!-- 2016-05-25T14:18:00 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="XmlDSReport_with_data" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="85d7b9ad-6feb-43dc-84cc-5175bf629546">
<parameter name="xmlString" class="java.lang.String">
<defaultValueExpression><![CDATA["<?xml version=\"1.0\" encoding=\"UTF-8\"?><a><b><val>val1</val></b><b><val>val2</val></b></a>"]]></defaultValueExpression>
</parameter>
<parameter name="XML_INPUT_STREAM" class="java.io.InputStream">
<defaultValueExpression><![CDATA[new java.io.ByteArrayInputStream($P{xmlString}.getBytes("UTF-8"))]]></defaultValueExpression>
</parameter>
<queryString language="xPath">
<![CDATA[/a/b]]>
</queryString>
<field name="value" class="java.lang.String">
<fieldDescription><![CDATA[val]]></fieldDescription>
</field>
<columnHeader>
<band height="31" splitType="Stretch">
<staticText>
<reportElement x="150" y="0" width="100" height="30" uuid="b33a123d-8987-4da4-b21b-1f9ccc50e92d"/>
<text><![CDATA[value]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="30" splitType="Stretch">
<textField>
<reportElement x="150" y="0" width="100" height="30" uuid="14c51219-5ce2-47ce-abb9-71bc11a6f28c"/>
<textFieldExpression><![CDATA[$F{value}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
After you deploy the report and create the input control for the xmlString
parameter you can test it. Let's say you want to pass this XML instead of leaving the default in place:
<?xml version="1.0" encoding="UTF-8"?>
<a>
<b><val>new_val1</val></b>
<b><val>new_val2</val></b>
</a>
To test the reports service you run something similar to this in a terminal(I URL-encoded the XML string) and check the result:
curl -u user:password \
http://localhost:8080/jasperserver/rest_v2/reports/reports/XmlDSReport_with_data.pdf?xmlString=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%0A%20%20%20%20%3Ca%3E%0A%20%20%20%20%20%20%3Cb%3E%3Cval%3Enew_val1%3C%2Fval%3E%3C%2Fb%3E%0A%20%20%20%20%20%20%3Cb%3E%3Cval%3Enew_val2%3C%2Fval%3E%3C%2Fb%3E%0A%20%20%20%20%3C%2Fa%3E > report.pdf
To test the reportExecutions service, the main steps are:
1.Create an XML file with the request(name it reportExecutionRequest.xml
)
<reportExecutionRequest>
<reportUnitUri>/reports/XmlDSReport_with_data</reportUnitUri>
<outputFormat>pdf</outputFormat>
<freshData>true</freshData>
<saveDataSnapshot>false</saveDataSnapshot>
<interactive>true</interactive>
<allowInlineScripts>true</allowInlineScripts>
<async>false</async>
<parameters>
<reportParameter name="xmlString">
<value><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<a><b><val>new_val1</val></b><b><val>new_val2</val></b></a>]]></value>
</reportParameter>
</parameters>
</reportExecutionRequest>
2.Make the request(you need to save the session cookie to retrieve the output):
curl -u user:password \
-H "Content-Type: application/xml" \
-d @reportExecutionRequest.xml \
-c cookies.txt \
http://localhost:8080/jasperserver/rest_v2/reportExecutions
3.Get the output with the requestID
and exportID
from the result of the previous request:
curl -b cookies.txt \
http://localhost:8080/jasperserver/rest_v2/reportExecutions/cc57b351-cfb6-429e-8c92-d0aebebbed66/exports/b71d6353-1eec-4304-8713-5d0f3105680e/outputResource > report.pdf
With data as source URL
It is the same report template, but with the two parameters replaced with:
<parameter name="xmlSource" class="java.lang.String">
<defaultValueExpression><![CDATA["http://serverwithdata/xmlData.xml"]]></defaultValueExpression>
</parameter>
<parameter name="net.sf.jasperreports.xml.source" class="java.lang.String">
<defaultValueExpression><![CDATA[$P{xmlSource}]]></defaultValueExpression>
</parameter>
Note: I created two parameters here just because I wanted to keep a shorter name for the parameter when passing it through the reports service. I also created an input control just for the xmlSource
parameter.
The tests in this case are similar.
EDIT: To use JSON instead of XML, the original JasperReports template needs to be adjusted in this way:
For data as parameter, just change the xmlString
parameter, the XML_INPUT_STREAM
parameter and the queryString
to this:
<parameter name="jsonString" class="java.lang.String">
<defaultValueExpression><![CDATA["{\"a\": [ {\"b\": { \"val\": \"val1\"}}, {\"b\": { \"val\": \"val2\" }}]}"]]></defaultValueExpression>
</parameter>
<parameter name="JSON_INPUT_STREAM" class="java.io.InputStream">
<defaultValueExpression><![CDATA[new java.io.ByteArrayInputStream($P{jsonString}.getBytes("UTF-8"))]]></defaultValueExpression>
</parameter>
<queryString language="json">
<![CDATA[a.b]]>
</queryString>
For data as source URL, change the xmlString
parameter, the XML_INPUT_STREAM
parameter and the queryString
to this:
<parameter name="jsonSource" class="java.lang.String">
<defaultValueExpression><![CDATA["http://serverwithdata/jsonData.json"]]></defaultValueExpression>
</parameter>
<parameter name="net.sf.jasperreports.json.source" class="java.lang.String">
<defaultValueExpression><![CDATA[$P{jsonSource}]]></defaultValueExpression>
</parameter>
<queryString language="json">
<![CDATA[a.b]]>
</queryString>
The cURL tests for the REST services are basically the same with the main difference that you will be passing JSON instead of XML and use the JSON specific parameters jsonString
or jsonSource
.

- 2,421
- 4
- 16
- 23
-
I found a way to solve the problem. I use Remote XML, which is described [here](http://community.jaspersoft.com/wiki/xml-datasources-jaspersoft-studio). There is a parameter `XML_URL` that allows you to dynamically set the URL of Data source. This parameter can be set during the description of the `reportExecutionRequest` request, but last thing, which I do not understand how I should download the file from the server. When I send a request, as you write in paragraph 3, I get a response from the server description request status. How do I download the file directly on my laptop? – Sergey Sigaev May 26 '16 at 14:14
-
The `XML_URL` parameter is used in the deprecated `RemoteXmlDataAdapterService`, so use with caution. With `reportExecutions` service, getting the output of a report involves at least two steps: making the request and getting the output. I've just followed the latest guide [here](http://community.jaspersoft.com/documentation/tibco-jasperreports-server-rest-api-reference/v621/reportexecutions-service#Requesting_Report_Output). To get the file in one step, just use the simpler `reports` service. – Narcis May 26 '16 at 14:43
-
You should have everything you need in my answer. Just follow the example step by step and consult the official REST API guide. I strongly recommend doing things manually(in a command line, to see them working) before moving up to your python API. If you really do not succeed please post your commands with output in GitHub gist and add link to it. – Narcis May 26 '16 at 17:45
-
Extract of the report - a separate issue. Let's leave it. When I try to run a report from the way (`With data as source URL`)you describe and pass correct url to filling report, I get null values for fields. I do not understand why. My jrxml file [here](http://pastebin.com/0edV1pjC) – Sergey Sigaev May 26 '16 at 22:19
-
If you are seeing `null` values in Studio when previewing, just change the data adapter from **One Empty Record** to another one like **Sample DB**. Normally, you should preview it with no data or data adapter, but unfortunately there is no option for that.. yet. – Narcis May 27 '16 at 08:00
-
Error is not only in JasperSoft Studio. When I try to run a report with parameter `xmlSource=http://feeds.bbci.co.uk/news/rss.xml` i get an error with the following description: `net.sf.jasperreports.engine.JRException: Error opening input stream from URL: http://serverwithdata/xmlData.xml`. – Sergey Sigaev May 27 '16 at 09:02
-
I send a request to the following URL `http://localhost:8081/jasperserver-pro/rest_v2/reports/reports/BBCReport.xls?xmlSource=http://feeds.bbci.co.uk/news/rss.xml` – Sergey Sigaev May 27 '16 at 09:04
-
I've run your jrxml sample with that URL in Studio and Server and had no issues. You may be experiencing networking issues. Is this URL(http://feeds.bbci.co.uk/news/rss.xml) accessible from a browser in the same network with your server? – Narcis May 27 '16 at 10:11
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113127/discussion-between-geekcristiano-and-narcis). – Sergey Sigaev May 27 '16 at 10:13
The API is separated in three domains:
- Administration services
- Repository services
- Report services
To generate reports, they first have to be deployed to the server via the repository service. If later on the report needs to be generated, it is called via the report services. Here, it is also possible to start synchronous or asynchronous report generation.
Therefore the report needs to be in the repository.
EDIT
Since you also need to deliver parameters, referring to this example it would work like this:
- Add a report parameter
- Add a input control for a report parameter
- Call the API with your input parameter:
http://<host>:<port>/jasperserver[-pro]/rest_v2/reports/reports/samples/EmployeeAccounts.html?EmployeeID=sarah_id
- Do as needed for other parameters

- 8,033
- 6
- 26
- 41