使用 REST API 生成 JasperServer 报告时传入内容
我正在开发一个项目,旨在用 JasperReports Server 替换我们当前的 PDF 生成器。该计划是使用 REST/HTTP API 在系统之间达到高级抽象。
最佳情况下,我们不想让 JasperReports Server 从数据库中提取数据,因为这会绕过调用应用程序体系结构中现有的日志记录和授权。相反,我们希望首先提取调用应用程序中的内容,然后将该内容传递到 JasperReports Server。
我们已经做了相当多的调查,但缺乏相关结果表明这不是您通常使用 JasperReports Server 的方式。我们在教程中发现的输入参数通常是标量值(整数、布尔值或字符串),而不是复杂的结构或对象。此外,似乎每个示例都或多或少地假设您想让 JasperReports Server 连接到数据库。
如果可以传递复杂的结构(例如地图数组,其中一些地图元素是数组或地图本身),那么执行此操作的最佳实践是什么?我不知道如何在请求正文中格式化这样的结构。 SOAP API 是否更适合?
如果这根本不是您应该如何设计 JasperReports Server 解决方案,那么哪些替代产品/解决方案更合适?
预先感谢您的任何意见。
I'm working on a project that aims to replace our current PDF generator with JasperReports Server. The plan is to use the REST/HTTP API to reach a high level of abstraction between the systems.
Optimally, we do not want to let JasperReports Server pull the data from the database, since this would bypass the existing logging and authorization in the calling application's architecture. Instead, we'd like to start with extracting the content in the calling application, and then pass that content to JasperReports Server.
We've done quite a bit of investigating, and the lack of relevant results indicates that this is not how you typically use JasperReports Server. The input parameters in the tutorials we've found are typically scalar values (integers, booleans or strings), and not complex structures or objects. Furthermore, it seems like more or less every sample assumes that you want to let JasperReports Server connect to a database.
If it's possible to pass in complex structures (like an array of maps, where some map elements are arrays or maps themselves), what's the best practice for doing this? I have no idea of how such a structure should be formatted in the request body. Is the SOAP API a better fit?
If this is not at all how you should design a JasperReports Server solution, what alternative products/solutions are more suitable?
Thanks in advance for any input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过几个小时的研究后,我想我已经准备好回答我自己的问题了。
JasperReports Server(以下简称“JRS”)从根本上设计为自行获取数据。尽管可以强制向 JRS 提供数据,但我决定不这样做。
不让 JRS 本身获取数据的最明显缺点之一是无法再从 JRS Web 界面生成报告。如果客户端应用程序负责以预定义格式提供数据,那么与其他系统的集成也将变得不可能或困难。
在我正在进行的项目中,我们决定构建一个基于远程 XML 数据源的自定义 JRS 数据源,它调用客户端应用程序的 XML API。换句话说,客户端应用程序向 JRS 请求报告,然后 JRS 向客户端应用程序请求其数据。 XML API 必须进行扩展才能满足我们所有的报告需求,但在我看来这是一件好事。良好的 API 覆盖率将来会派上用场。
我希望这些想法可以帮助有类似问题的人。
After several hours spent on research, I think I'm ready to answer my own question.
JasperReports Server ("JRS" below) is fundamentally designed to fetch data by itself. Although it would be possible to force feed JRS with data, I've decided not to.
One of the most obvious drawback of not letting JRS fetch the data itself is that it would no longer be possible to generate reports from the JRS web interface. Integration from other systems also becomes impossible or difficult if the client application is responsible for supplying the data in a predefined format.
In the project I'm working on, we've decided to build a custom JRS DataSource based on the Remote XML DataSource, that invokes the client application's XML API. In other words, the client application requests a report from JRS, and JRS then requests it's data from the client application. The XML API will have to be expanded to cover all of our reporting needs, but that's a good thing in my opinion. Good API coverage will come in handy in the future.
I hope these thoughts helps someone having similar questions.
正如您所写,对于 JRS 来说,获取数据是更自然的方式。然而,我需要采取相反的方式 - 我通过 REST 调用发布数据以报告 JRS 存储库中的情况。
我在参数“xmlDocument”中传递 XML 数据,并且通过“技巧”,执行的报告可以使用此 XML 进行进一步的 X 路径查询。
xmlDocument 只是一个简单的字符串:
在设计阶段,我使用用于预览的 XML 文件创建 XML 数据适配器。请注意,选择 XML 适配器后出现了一个新参数 XML_INPUT_STREAM。
然后我将报告发布给 JRS。
在报表执行期间,当报表未链接到任何数据源时,它会读取 XML_INPUT_STREAM 参数(作为后备数据源),如下所示:
我将“xmlDocument”字符串包装到 InputStream。
As you wrote, fetching data is more natural way for JRS. However, I needed to go the opposite way - I POST data to report sitting in JRS repo via a REST call.
I pass XML data in my parameter "xmlDocument" and, by means of a "trick", an executed report can use this XML for further X-path queries.
xmlDocument is just a simple String:
At designing phase I create XML data adapter with XML file that I use for previewing. Note that a new parameter XML_INPUT_STREAM appeared after choosing XML adapter.
Then I publish the report to JRS.
During report execution, when the report is not linked to any data source, it reads XML_INPUT_STREAM parameter instead (as fallback data source), that looks as follows:
I wrap "xmlDocument" string to InputStream.
即使我同意这个答案,女巫说 JasperServer 是为了自己获取数据而构建的,我仍然必须通过其余 API 传递数据,因为这是我公司构建 Jasper 报告的传统方式,并且因为我们想要使用自定义 Java 服务来获取数据。
我发现上面描述的方法是最简单的方法。
拥有这个简单的自定义 pojo,您希望通过 Web API 传递到报告:
1) 定义一个自定义 jasper scriptlet,该脚本必须作为与报告相关的资源部署在服务器上,女巫会将字符串反序列化为自定义 pojo 对象使用 GSON:
2) 在 jasper 服务器中使用带有自定义 scriptlet 的参数/变量:
3) 调用 API @ jasperserver/rest_v2/reportExecutions,如下所示:
Even if I agree with the answer, witch states that the JasperServer has been built to fetch data by itself, I had still to pass the data trough the rest API because it's the legacy way of my company to build Jasper reports and because we want to use custom Java services to fetch data.
I've found this described above to be the simpliest possible way to do this.
Having this simple custom pojo that you want to pass to the report trough web API:
1) Define a custom jasper scriptlet that has to be deployed on the server as a resource related to the report, witch will deserialize the string into the custom pojo object using GSON:
2) Use the parameter/variable with the custom scriptlet in jasper server:
3) Invoke the API @ jasperserver/rest_v2/reportExecutions like this: