Reputation: 276
I have a Spring project. I am also using JasperReport to build report templates for my Spring project to call, use and generate.
After looking through a couple of tutorials, I finally managed to get my Spring project to call and generate my report template.
But one thing I'm not familiar with is that I've configured my Jasper Reports with a dataset query which populates the template with data when I preview it within JasperSoft. But when I call this same report.jasper
file through Spring, all I got was an empty report(albeit with some expected formatting)
Most of the examples I've come across has their JasperReport use an empty datasource while they passed in the required data from the db through Spring. But my report is making use of multiple dataset queries (and thus using multiple subtables), so I'm not sure where do I go from here; whether I need to manually pass in the data through Spring like those examples or can I depend on the JasperReport to do so.
I'm still relatively new to both Jasper and Spring.
Created based on this link, this is a sample of my test function:
@GetMapping(path = "/report")
void testFunction2(HttpServletResponse response) throws IOException, JRException {
String sourceFileName = new File("C:\\\\Users\\\\User\\\\JaspersoftWorkspace\\\\MyReports\\\\base3.jasper").getAbsolutePath();
JRBeanCollectionDataSource sampleDataSource = new JRBeanCollectionDataSource(null);
String testName = "testName.xlsx";
HashMap<String, Object> theHashMap = new HashMap<>();
JasperPrint jasperPrint = JasperFillManager.fillReport(sourceFileName, theHashMap);
JRXlsxExporter exporter = new JRXlsxExporter();
SimpleXlsxReportConfiguration reportConfigXLS = new SimpleXlsxReportConfiguration();
reportConfigXLS.setSheetNames(new String[] { "sheet1" });
exporter.setConfiguration(reportConfigXLS);
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
response.setHeader("Content-Disposition", "attachment;filename="+testName);
response.setContentType("application/octet-stream");
exporter.exportReport();
}
Upvotes: 0
Views: 1893
Reputation: 276
I think I solved my issue/confusion.
The most important thing I needed was a connection to my DB. Most of the tutorials has their datasource connected from within the Spring project itself, make a repository call and send the data from the call to the report to be generated.
In my case, I'm not calling the DB from the Spring side, but from the Jasper side.
So, alternatively, and the solution to my issue, is that I could just send the connection details using DriverManager.getConnection()
and parsing this as the 3rd variable to fillReport
as opposed to the datasource.
Basically, what everyone else is doing
JRBeanCollectionDataSource sampleDataSource = new JRBeanCollectionDataSource(repositoryService.getData());
JasperPrint report = JasperFillManager.fillReport(compiledReport, parameters, sampleDataSource);
But what I want is this
Connection con = DriverManager.getConnection("jdbc:postgresql://url:port/dbName","dbUsername","dbPassword");
JasperPrint jasperPrint = JasperFillManager.fillReport(compiledReport, parameters, con);
(but both methods are correct)
Once I did that 2nd method, my report data is successfully generated using all the pre-configured SQL queries within JasperSoft.
I got the idea from here
Upvotes: 1