迁移一个使用 JasperReports 的项目从 Spring 4 到 Spring 5。

huangapple go评论133阅读模式
英文:

Migrating a project with JasperReports from Spring 4 to Spring 5

问题

大家好,我们计划将项目从Java 8升级到Java 11。因此,项目的Spring版本将从Spring 4x更改为Spring 5x。

在Spring 4x中,视图解析如下所示:

  1. Spring XML配置:
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
    <property name="location" value="/WEB-INF/jasper-view.xml"/>
    <property name="order" value="0"/>
</bean>
  1. jasper-view.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!--在这里,所有的url值都应包含jrxml文件的有效路径-->

    <bean id="purchasePdf"
          class="org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView"
          p:url="classpath:reports/purchaseReport.jrxml"
          p:reportDataKey="datasource" />

</beans>

因此,随着升级,Spring版本更新为5.2.5.RELEASE,jasper版本更新如下:

<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>6.4.0</version>
</dependency>

该项目在不报错的情况下构建,但在Tomcat服务器部署时出现以下错误:

Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView] for bean with name 'purchasePdf' defined in ServletContext resource [/WEB-INF/jasper-view.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView

似乎Spring 5已经放弃了对Jasper的支持,是否有其他替代解决方案?

https://github.com/spring-projects/spring-framework/issues/17884

英文:

Hi all we are planning to upgrade our project from Java 8 to Java 11. Therefore Spring version of the project shall be changed from Spring 4x to Spring 5x

The view resolving was done in Spring 4x as follows

  1. spring xml configuration:

    <bean class="org.springframework.web.servlet.view.XmlViewResolver">
    <property name="location" value="/WEB-INF/jasper-view.xml"/>
    <property name="order" value="0"/>
    </bean>

  2. jasper-view.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd&quot;>

     &lt;!--here all the url value should contains the valid path for the jrxml file--&gt;
    
     &lt;bean id=&quot;purchasePdf&quot;
           class=&quot;org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView&quot;
           p:url=&quot;classpath:reports/purchaseReport.jrxml&quot;
           p:reportDataKey=&quot;datasource&quot; /&gt;
    

So with the upgrade Spring version was updated to 5.2.5.RELEASE and jasper version was updated as follows:

&lt;dependency&gt;
    &lt;groupId&gt;net.sf.jasperreports&lt;/groupId&gt;
    &lt;artifactId&gt;jasperreports&lt;/artifactId&gt;
    &lt;version&gt;6.4.0&lt;/version&gt;
&lt;/dependency&gt;

The project is building without and error but gives the following error when deploying in Tomcat server:

Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView] for bean with name &#39;purchasePdf&#39; defined in ServletContext resource [/WEB-INF/jasper-view.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView

It seems Spring 5 has dropped it's Jasper support

> https://github.com/spring-projects/spring-framework/issues/17884

Is there an alternative solution for this?

答案1

得分: 1

它建议使用原生的JasperReports API:

> 因此,我们更建议在Spring MVC处理程序方法中原生使用JasperReports API,从特定设计的RESTful端点生成报告。自Spring Framework 5.0起,我们将放弃现有的几乎无用的JasperReports视图类层级结构。请注意,尽管在Spring Framework 4.3.x系列中仍保留了对已弃用的JRExporter API的支持,直到2019年,特别是针对现有应用程序。然而,即使在4.3版本中,也值得考虑原生使用JasperReports API。

我想这些都是进行升级时的挑战和困扰的一部分。

英文:

It recommends to use the native JasperReports API:

> As a consequence, we rather recommend native use of the JasperReports
> API in Spring MVC handler methods, generating reports from
> specifically designed RESTful endpoints. We are dropping our now
> semi-useless JasperReports view class hierarchy as of Spring Framework
> 5.0. Note that our existing support against the deprecated JRExporter API remains around in the Spring Framework 4.3.x line until 2019, in
> particular for existing applications. However, even with 4.3, native
> use of the JasperReports API is worth considering.

I suppose these are part of the challenges / pains of doing an upgrade.

huangapple
  • 本文由 发表于 2020年4月9日 20:37:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61121327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定