java - 当尝试从数据适配器检索 bean 时,为什么会出现错误 (ClassCastException)?

标签 java jasper-reports classcastexception jaspersoft-studio

我使用 JasperSoft Studio 创建了一份报告。

我引用了这篇有用的帖子 Jaspersoft Studio: How to use Collection of Java Beans in data adapter通过 Alex K

我想检索订单列表。类 Order 的定义如下:

public class Order {
    private double price;
    private int quantity;
    private Product product;

    // Getters & Setters
}

如果我想检索价格数量,报告就会成功生成。但是,一旦我检索产品,就会显示:

Error at JSS

详细信息:

net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression for source text: $F{product}.getName()
    at com.jaspersoft.studio.editor.preview.view.control.ReportControler.fillReport(ReportControler.java:530)
    at com.jaspersoft.studio.editor.preview.view.control.ReportControler.access$20(ReportControler.java:505)
...
Caused by: java.lang.ClassCastException: ru.alex.Product cannot be cast to ru.alex.Product
    at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:277)
    ... 14 more

文件sample.jrxml是:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-05-18T13:29:14 -->
<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="sample" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ade9e357-e2d0-42bb-ae0d-000b69f4e2e9">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="JavaBeanCollection - orders"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="product" class="ru.alex.Product">
        <fieldDescription><![CDATA[product]]></fieldDescription>
    </field>
    <field name="quantity" class="java.lang.Integer">
        <fieldDescription><![CDATA[quantity]]></fieldDescription>
    </field>
    <field name="price" class="java.lang.Double">
        <fieldDescription><![CDATA[price]]></fieldDescription>
    </field>
    <title>
        <band height="80" splitType="Stretch">
            <staticText>
                <reportElement x="70" y="49" width="100" height="30" uuid="a19f5b7c-21ed-4c00-a224-af5cf7ef27ec"/>
                <text><![CDATA[price]]></text>
            </staticText>
            <staticText>
                <reportElement x="170" y="49" width="100" height="30" uuid="772c4807-25f7-4e7a-8a10-eba5232b92c7"/>
                <text><![CDATA[quantity]]></text>
            </staticText>
            <staticText>
                <reportElement x="270" y="49" width="140" height="30" uuid="613da9ef-6a5a-45b2-8c8f-c3cd450e66ed"/>
                <text><![CDATA[product]]></text>
            </staticText>
        </band>
    </title>
    <detail>
        <band height="130" splitType="Stretch">
            <textField>
                <reportElement x="70" y="0" width="100" height="30" uuid="a594224b-c015-4dab-b52b-6e317e76cea3"/>
                <textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="170" y="0" width="100" height="30" uuid="b60503ca-f6bc-48dc-ad01-178d9befd805"/>
                <textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="270" y="0" width="140" height="30" uuid="480bfb2f-2831-4700-8adc-f818bbbf6592"/>
                <textFieldExpression><![CDATA[$F{product}.getName()]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

JRBeanCollection 如下:

public class MyImplementation implements JRDataSource {
    // (...)
    public static Collection<Order> getOrders() {
        List<Order> orders = new ArrayList<Order>();
        orders.add(new Order(1, "aa", new BigDecimal("1111.11"), 2, new Product("apples")));
        orders.add(new Order(2, "bb", new BigDecimal("2222.22"), 10, new Product("oranges")));

        return orders;
        }
    // (...)
}

您能告诉我异常的原因是什么吗?

最佳答案

它看起来像是 Jaspersoft Studio (JSS) 的错误。

我认为得到ClassCastException的原因(看看堆栈跟踪的这一部分:Caused by: java.lang.ClassCastException: ru.alex.Product无法转换为ru.alex .Product)使用两个 jar 文件:

  • 第一个jar是为数据提供者设置的;
  • 第二个 - 通过项目的构建路径。

是的,它是具有相同类的相同jar文件(物理上)。看起来像是类路径中有多个 jar 的问题。

了解问题的根源后,我们可以很容易地解决这个问题。

我们的 bean 类应该只保留一个 jar - 在 JSS 构建路径中: Build path at JSS

这意味着我们应该从数据适配器的属性中删除jar。像这样:

Data adapter dialog

此后最好重新启动JSS

对于这个jrxml:

<?xml version="1.0" encoding="UTF-8"?>
<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="Report with Bean" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="JavaBeanCollection - orders"/>
    <field name="product" class="ru.alex.Product">
        <fieldDescription><![CDATA[product]]></fieldDescription>
    </field>
    <field name="quantity" class="java.lang.Integer">
        <fieldDescription><![CDATA[quantity]]></fieldDescription>
    </field>
    <field name="price" class="java.lang.Double">
        <fieldDescription><![CDATA[price]]></fieldDescription>
    </field>
    <detail>
        <band height="30" splitType="Stretch">
            <textField>
                <reportElement x="10" y="0" width="100" height="30"/>
                <textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="110" y="0" width="100" height="30"/>
                <textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="210" y="0" width="100" height="30"/>
                <textFieldExpression><![CDATA[$F{product}.getName()]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

我们正在JSS成功获取报告:

Report at JSS

<小时/>

Java 项目中一切正常(没有任何黑魔法),因为我们在类路径中只有一个 jar 和我们的 bean 类。

关于java - 当尝试从数据适配器检索 bean 时,为什么会出现错误 (ClassCastException)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44047133/

相关文章:

java - 如何在 Selenium Web 驱动程序中编写等待,直到下拉列表中的值被填充,然后单击按钮

java - Excel 2010 收到有关关闭工作簿的警报

java - 第二次调用 FragmentTransaction.replace() 时出现 Android Fragment 转换错误

java - 列表中包含可点击组件的通用 ListCellRenderer 不起作用

grails - 如何在Jasper报告中导出PDF报告

jasper-reports - 第二次登录后 Visualize.js 身份验证错误

Java jasper 报告 valueAxis 位置

java - ClassCastException:RestTemplate 返回 List<LinkedHashMap> 而不是 List<MymodelClass>

Java.Lang.Object,无法转换为 Backed.Bean

java - Spring Security允​​许每个用户看到他们自己的个人资料,但没有其他人可以查看