Web Service DS in JasperReports Sample (using Plain Old Java)

Hi there!

The WebService Data Source works like a charme inside the Jaspersoft Studio and in the JasperReports Server Pro.
Now I try to ceate a basic Java Class which should create the report using the API.

And here is my problem at all - is there any example available for generating a PDF file out of a report which uses the Web Service Data Source?

I struggle with the Fill Manager...

JasperFillManager.fillReportToFile("reports/SampleReport.jasper", reportParams,
                <And What goes here?>);
I tried the WSEmptyDatasource, but this seems to be the wrong one.
I used the 
WebServiceDataAdapterServiceFactory to contribute the Report Parameters Map and this seems to be working.
I feel close to a solution but I cannot see one actually :)
 
Thank you very much!

Best regards,
Kevin
 
kevin.haferkamp's picture
Joined: May 10 2013 - 2:47am
Last seen: 5 years 5 months ago

With this I'll get a NPE at com.jaspersoft.webservice.data.query.WebServiceQueryExecuter.getInternalQE(WebServiceQueryExecuter.java:243)

complete sample:

public class JasperReportsGenerator {
 
    public static void main(String[] args) {
        // Instantiate Web Service Data Source Data Adapter
        WebServiceDataAdapterImpl wsDataAdapter = new WebServiceDataAdapterImpl();
        wsDataAdapter.setName("WS DS NAME");
        wsDataAdapter.setWsUri("http://localhost:8090/some/service");
        wsDataAdapter.setVerb(VerbType.GET.toString());
        wsDataAdapter.setLanguage(LanguageType.JSON);
        wsDataAdapter.setAuthType(AuthType.NONE);
 
        SimpleJasperReportsContext jasperReportsContext = new SimpleJasperReportsContext();
 
        jasperReportsContext.setProperty("webservice",
                "com.jaspersoft.webservice.data.query.WebServiceQueryExecuterFactory");
        jasperReportsContext.setProperty("WebServiceQuery",
                "com.jaspersoft.webservice.data.query.WebServiceQueryExecuterFactory");
 
        WebServiceDataAdapterServiceFactory factory = WebServiceDataAdapterServiceFactory
                .getInstance();
 
        WebServiceDataAdapterService dataAdapterService = (WebServiceDataAdapterService) factory
                .getDataAdapterService(jasperReportsContext, wsDataAdapter);
        dataAdapterService.dispose();
 
        // Instantiate report properties map (REPORT_PARAMETERS)
        Map<String, Object> reportParams = new HashMap<String, Object>();
 
 
        try {
            dataAdapterService.contributeParameters(reportParams);
            System.out.println("Filling Reports...");
            long start = System.currentTimeMillis();
            JasperFillManager.fillReportToFile("reports/SampleReport.jasper", reportParams);
            System.out.println("Filling time: " + (System.currentTimeMillis() - start));
 
            System.out.println("Exporting to PDFs...");
            start = System.currentTimeMillis();
 
            File sourceFile = new File("reports/SampleReport.jrprint");
            JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile);
 
            File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".pdf");
 
            JRPdfExporter exporter = new JRPdfExporter();
            exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
            exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(destFile));
            SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
            exporter.setConfiguration(configuration);
            exporter.exportReport();
 
            System.out.println("Exporting time: " + (System.currentTimeMillis() - start));
        } catch (JRException | IOException e) {
            System.err.println("Error in executiong the reports!");
            System.err.println(e);
    }
 
}

kevin.haferkamp - 3 years 8 months ago

5 Answers:

I have the same problem.

I try to write code for my servlet, but i didn't find docs.

I have created jrxml from jasper studio and work great, but i do not have example to use it in my java code.

I try with kavinb code without success. Can someone share docs or code?

Best regards.

Marcello 

info_477's picture
Joined: Jan 2 2017 - 3:44pm
Last seen: 6 years 2 months ago

Does anyone have a working code for this or any other solution?

 

Best regards,
Edwin L
 
edwinl182's picture
Joined: Jan 6 2014 - 11:16pm
Last seen: 5 years 4 months ago

Seconding Edwin L, is there a working example of running a report from java? My reports run just fine from within JaspersoftStudio, but am getting the same NPE as described above by Kevin.

Any help would be appreciated,

Howard

Here's some additional info,

 - I was able to trace the NPE to be in the WebServiceQueryExecuter.class, it tries to connect to the web service providing the data for the report. Based on the debugger, all of the urls and parameters for the web service are being pulled in correctly from the report file (.jasper).

 - To do the connection, it uses the method 'callWS' from the WebServiceConnector.class. This method returns a null, which then causes the exception quoted in the issue 'com.jaspersoft.webservice.data.query.WebServiceQueryExecuter.getInternalQE(WebServiceQueryExecuter.java:243)'.

Here is the full stack trace, if that helps,

SEVERE: Servlet.service() for servlet [PDFViewer] in context with path [/PCAReportViewer] threw exception
java.lang.NullPointerException
at com.jaspersoft.webservice.data.query.WebServiceQueryExecuter.getInternalQE(WebServiceQueryExecuter.java:243)
at com.jaspersoft.webservice.data.query.WebServiceQueryExecuter.close(WebServiceQueryExecuter.java:200)
at net.sf.jasperreports.engine.fill.JRFillDataset.closeQueryExecuter(JRFillDataset.java:1322)
at net.sf.jasperreports.engine.fill.JRFillDataset.closeDatasource(JRFillDataset.java:1303)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:608)
at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:163)
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:474)
at net.sf.jasperreports.engine.JasperFillManager.fillToFile(JasperFillManager.java:259)
at net.sf.jasperreports.engine.JasperFillManager.fillReportToFile(JasperFillManager.java:746)
at PDFViewer.doGet(PDFViewer.java:89)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
 
Also, as a reference, here are the jar files that I've pulled into my servlet:
commons-beanutils-1.9.0
commons-collections-3.2.2
commons-digester-2.1
commons-logging-1.1.1
customDatasource_WebService_JRS_wrapper
groovy-all_2.4.5
httpcore-4.3.3
iText-2.1.7.js2
jackson-annotations-2.4.3
jackson-core-2.4.3
jackson-databind-2.4.3
jasperreports-6.3.1
WebServiceDataAdapter

 

hjohnson_1's picture
Joined: May 9 2017 - 9:05am
Last seen: 5 years 6 days ago

Hi,

i have the some propleme....if any one have a solution and can help !!!!

oussama.sghaier's picture
Joined: Jun 13 2017 - 2:10am
Last seen: 5 years 9 months ago

Whew, that was a dilly of a problem. As it turns out the Web Service DataSource is notoriously bad at letting you know what's going on. As it turned out, once I added httpclient-4.3.6.jar to my build path, it worked!!

So, the final list of jars that I added to the build path are:

commons-beanutils-1.9.0
commons-collections-3.2.2
commons-digester-2.1
commons-logging-1.1.1
customDatasource_WebService_JRS_wrapper
ElasticSearchConnector
groovy-all_2.4.5
httpclient-4.3.6
httpcore-4.3.3
iText-2.1.7.js2
jackson-annotations-2.4.3
jackson-core-2.4.3
jackson-databind-2.4.3
jasperreports-6.3.1
WebServiceDataAdapter
xalan-2.7.1
 
I hope this helps.

 

hjohnson_1's picture
Joined: May 9 2017 - 9:05am
Last seen: 5 years 6 days ago
Feedback
randomness