用于在JasperServer中填写报告的位置数据 [英] Location data for filling the report in JasperServer

查看:219
本文介绍了用于在JasperServer中填写报告的位置数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JasperSoft Studio中开发了一个模板设计,并将 jrxml 文件上传到JasperServer。我想从我的python应用程序发送数据(JSON或XML)到填充报告模板,并使用REST API以一些流行的格式(如PDF,XLS)收回报告。 我不想将数据存储在服务器上。我怎样才能做到这一点?或者数据必须存储在服务器上并且没有其他方式通过WEB服务进行传输?

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?

推荐答案

数据不一定必须驻留在服务器上。

The data does not necessarily have to reside on the server.

您可以设计模板,使您可以通过输入控制参数传递数据@ tobi6建议。
然后您可以使用reports服务或reportExecutions服务来获得所需的输出。

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.

在您的情况下,数据可能是实际数据(XML或者JSON)或数据源(数据文件的URL)。

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).

以下是使用XML数据的一些基本示例(对于JSON非常相似) :

Here are some basic samples for working with XML data(for JSON is quite similar):

JasperReports模板:

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>

部署报告并为 xmlString创建输入控件参数你可以测试一下。假设您要传递此XML而不是保留默认值:

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>

要测试报告服务,您可以在终端中运行与此类似的内容(我对XML进行了URL编码)字符串)并检查结果:

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

要测试reportExecutions服务,主要步骤是:

To test the reportExecutions service, the main steps are:

1.使用请求创建XML文件(将其命名为 reportExecutionRequest.xml

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.发出请求(您需要保存会话cookie以检索输出):

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.使用 requestID exportID 来自上一个请求的结果:

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



将数据作为源URL



它是相同的报告模板,但将两个参数替换为:

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>

注意:我在这里创建了两个参数,因为我想保留一个传递报表服务时参数的较短名称。我还为 xmlSource 参数创建了一个输入控件。

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.

编辑:要使用JSON而不是XML,需要以这种方式调整原始的JasperReports模板:

EDIT: To use JSON instead of XML, the original JasperReports template needs to be adjusted in this way:

对于数据作为参数,只需更改 xmlString 参数, XML_INPUT_STREAM 参数和 queryString 到此:

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>

对于数据作为源网址,请更改 xmlString 参数, XML_INPUT_STREAM 参数和 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>

REST服务的cURL测试与您传递JSON的主要区别基本相同而不是XML并使用JSON特定参数 jsonString jsonSource

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.

这篇关于用于在JasperServer中填写报告的位置数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆