Jasperreports从Jaspersoft Studio中带有JSON的基本报告迁移到Java代码呈现

时间:2019-01-08 22:55:01

标签: java jasper-reports

我同时设置了JSON和XML数据适配器,然后将其与数据源一起使用。我是用jasperreports studio列出JSON数组和字段并列出数据的表。当我运行预览时,JSON“文件”中的数据会正确呈现。

我希望能够在“ Studio”中使用该基本示例并将其转换为Java代码,以便我可以从流/字符串或文件中读取JSON输入,然后将其转换为PDF字节数组输出。

我创建了一个JSON文件数据适配器,然后完成了数据源的创建。

我在通过Java将数据传递到模板时遇到问题。当我尝试构建PDF文档时,结果显示为“ null”或空白字段。

示例JSON作为文件:

{
  "data": [    
    {
       "title": "Everyday Book",
       "author": "JK Rowling"
    },
    {
       "title": "Good Book",
       "author": "John Grisham"
    }
  ]
}

然后是Java文件(伪代码)

 // Load file into string
            iss = new ClassPathResource("/com/ca/services/it/reportgencore/bookstorej.json").getInputStream();            
            final StringWriter writer = new StringWriter();
            IOUtils.copy(iss, writer, "UTF-8");
            final String jsonAsString = writer.toString();
            System.out.println("JSON as input>>>: "+jsonAsString);

            targetStream = new ByteArrayInputStream(jsonAsString.getBytes());

            // Build a JSON datasource:
            //final JsonDataSource jsonStreamDataSource = new JsonDataSource(targetStream, ".");
            final JsonDataSource jsonStreamDataSource = new JsonDataSource(new File("C:\\projects\\workspaceJava8IspMicroServices\\pol-reportgen-lib\\src\\integration-tests\\java\\com\\ca\\services\\it\\reportgencore\\bookstorej.json"));

            //Use the compiled version
            final JasperReport report = TestJasperReportUtil.loadJASPERFile("/com/ca/services/it/reportgencore/Blank_A4_7.jasper");
            final JasperPrint jasperPrint = JasperFillManager.fillReport(report, params, jsonStreamDataSource);
            pdf = JasperExportManager.exportReportToPdf(jasperPrint);            
            dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("zzpdfoutput/test2.pdf")));
            ps = new PrintStream(dos, true);                   
            ps.write(pdf);

这是我缺少连接的部分,如何将json数据映射到模板:

<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="Blank_A4_7" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="9e99f12d-4afb-4cec-b91e-03355624b99b">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="bookstorejson"/>
    <queryString language="json">
        <![CDATA[data]]>
    </queryString>
    <field name="title" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="title"/>
        <fieldDescription><![CDATA[title]]></fieldDescription>
    </field>
    <field name="author" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="author"/>
        <fieldDescription><![CDATA[author]]></fieldDescription>
    </field>
    <group name="title">
        <groupExpression><![CDATA[$F{title}]]></groupExpression>
    </group>
...

<detail>
        <band height="53" splitType="Stretch">
            <textField>
                <reportElement x="50" y="0" width="100" height="30" uuid="b549bddf-e9ec-4c3e-87e4-66b1dcaf52d7"/>
                <textFieldExpression><![CDATA[$F{title}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="150" y="0" width="100" height="30" uuid="c803aba1-4fb6-4c3e-8f44-2e0de9ff6ca2"/>
                <textFieldExpression><![CDATA[$F{author}]]></textFieldExpression>
            </textField>
        </band>
    </detail>

bookstorejson是我针对json文件创建的数据适配器的名称,我是否需要以某种方式填充它。

<property name="com.jaspersoft.studio.data.defaultdataadapter" value="bookstorejson"/>

这是完整的更新jrxml,仍然不返回数据:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2019-01-09T11:40:09 -->
<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="JsonOrdersReport" pageWidth="500" pageHeight="842" columnWidth="500" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="5a62986f-c97c-4e2f-b4ed-d9f38960dab4">
    <property name="net.sf.jasperreports.json.source" value="data/northwind.json"/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <queryString language="json">
        <![CDATA[Northwind.Orders]]>
    </queryString>
    <field name="Id" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="OrderID"/>
    </field>
    <field name="OrderDate" class="java.util.Date">
        <property name="net.sf.jasperreports.json.field.expression" value="OrderDate"/>
    </field>
    <field name="ShipCity" class="java.lang.String">
        <property name="net.sf.jasperreports.json.field.expression" value="ShipCity"/>
    </field>
    <field name="Freight" class="java.lang.Float">
        <property name="net.sf.jasperreports.json.field.expression" value="Freight"/>
    </field>
    <variable name="TotalFreight" class="java.lang.Float" calculation="Sum">
        <variableExpression><![CDATA[$F{Freight}]]></variableExpression>
    </variable>
    <pageHeader>
        <band height="14">
            <frame>
                <reportElement mode="Opaque" x="0" y="2" width="356" height="10" forecolor="#CCFFFF" backcolor="#CCFFFF" uuid="e9af134f-31eb-48be-bd9b-292188f2554f"/>
                <staticText>
                    <reportElement mode="Opaque" x="0" y="0" width="48" height="10" backcolor="#CCFFFF" uuid="62e5e770-7b05-4ecd-a254-ab0c7f643a37"/>
                    <textElement textAlignment="Right"/>
                    <text><![CDATA[ID]]></text>
                </staticText>
                <staticText>
                    <reportElement mode="Opaque" x="54" y="0" width="87" height="10" backcolor="#CCFFFF" uuid="c472f825-47f4-4e16-a782-cc4b02572cb0"/>
                    <textElement textAlignment="Center"/>
                    <text><![CDATA[Order Date]]></text>
                </staticText>
                <staticText>
                    <reportElement mode="Opaque" x="146" y="0" width="108" height="10" backcolor="#CCFFFF" uuid="89b5edba-1606-4f5d-89cb-144042c1fcdd"/>
                    <text><![CDATA[Ship City]]></text>
                </staticText>
                <staticText>
                    <reportElement mode="Opaque" x="259" y="0" width="92" height="10" backcolor="#CCFFFF" uuid="e7c6fbe3-ecb2-4c65-83d6-7c813448cec6"/>
                    <textElement textAlignment="Right"/>
                    <text><![CDATA[Freight]]></text>
                </staticText>
            </frame>
        </band>
    </pageHeader>
    <detail>
        <band height="14">
            <textField>
                <reportElement x="0" y="2" width="51" height="10" uuid="ec54687d-3c95-4647-9db5-fa71a6e81009"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA[$F{Id}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="yyyy, MMM dd">
                <reportElement positionType="Float" x="54" y="2" width="87" height="10" uuid="a112ba7b-c321-467c-91ec-ffb513c23338"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression><![CDATA[$F{OrderDate}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="146" y="2" width="108" height="10" uuid="6a61edb3-239e-4791-a046-a6459343ac07"/>
                <textFieldExpression><![CDATA[$F{ShipCity}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="¤ #,##0.00">
                <reportElement positionType="Float" x="259" y="2" width="92" height="10" uuid="61a8a117-6a43-46a7-9b96-10c5beb578ab"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA[$F{Freight}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <summary>
        <band height="77">
            <staticText>
                <reportElement x="126" y="40" width="100" height="30" uuid="15321182-a6c8-494d-ba0f-56e71987d588"/>
                <text><![CDATA[Simple Data]]></text>
            </staticText>
        </band>
    </summary>
</jasperReport>

0 个答案:

没有答案
相关问题