How to use xml data source on jasper server

2019-07-20 05:57发布

问题:

I would like to use xml data source in jasper server (5.0.0). Xml files are created "on the fly" while application is running so different reports will have different xml data sources. I know that jasper server does not have XML data source defined but I found out that creating report without data source and then passing parameter XML_FILE - java.io.File will do the work. I managed to do this in java servlet:

  jasperReport = JasperCompileManager.compileReport("path to jrxml");       
  HashMap map  = new HashMap();
  map.put("XML_FILE", new File(xmlSourceFile));     
  jasperPrint = JasperFillManager.fillReport(jasperReport,map);         
 byte [] o = JasperExportManager.exportReportToPdf(jasperPrint);

but unfortunately failed to do it on jasper server. I am using rest services to run report so I can use only String parameters. I've tried to write a scriptlet that converts String parameter with xml url to java.io.File

public class XmlScriplet extends JRDefaultScriptlet{
 @Override
 public void beforeReportInit(){
  try {
       String param = (String)this.getParameterValue("fileName");
       HashMap map  = new HashMap();
       map.put("XML_FILE", new File(param));
       this.parametersMap.putAll(map);

  } ...

but this gives me an empty report. Thank you in advance.

回答1:

You can also pass any XML resource (be an static XML or even a REST service) by passing by simply "net.sf.jasperreports.xml.source" parameter to your report, if you are using Jasperreports Server 5.5, and assuming your report unit is named "report" and your xml data resource is in http://(host):(port)/resource.xml path, you might want to call the report using Jasper's REST v2 API like this:

 http://(host):(port)/jasperserver/rest_v2/reports/path/to/your/report/unit/report.html?net.sf.jasperreports.xml.source=http://(host):(port)/resource.xml

In order to have a default "net.sf.jasperreports.xml.source" value in your report, You should also add the following in the "parameters" section in your report source jrxml

<parameter name="net.sf.jasperreports.xml.source" class="java.lang.String"> 
    <defaultValueExpression><![CDATA["http://(host):(port)/resource.xml"]]>   </defaultValueExpression>
</parameter>

Do not forget to add xpath2 query support to jasperreports server by appending:

# addition for xpath2 queries
net.sf.jasperreports.query.executer.factory.xpath2 = net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory

to /WEB-INF/classes/jasperreports.properties inside the deployed dir in the appserver

For more info about which parameters xml data source allows, you could also take a look at the official documentation



回答2:

The JasperSoft Community wiki has two articles that will help explain some of the details.

Remote XML Datasource

Using XML Datasource in JasperReports server

You may need experiment with the XML_URL parameter to get your XML into the report.