Jasper Reporting using Spring Boot

hariom sinha
2 min readMar 15, 2020

Of all the development of applications being done, a curative summary speaks of the data and end results of what any web application does. For this summary, reporting is one of the important aspects of telling the user, the final result obtained from the end-to-end flow of the application.

Jasper Report is one of those, XML-based reporting tools, which can be utilised for the development of a handful of reports. They are simple in development and need proper parameter mapping to embed data into it. The key thing one needs to keep in mind, is the use of these mappings in the Java-based applications, with the suitable data types. From the development of Tables to represent the visualisations, Jasper reports are very helpful for reporting purposes.

Any element present in the report must be properly mapped with a unique parameter name, that can be used in the application as a part of connection of the xml with the back end.

Using the controllers of Spring Boot, we can easily generate the reports in any defined URL. The POM ( Project Object Model) of Spring Boot provides us with an easy way to declare the dependency for the Jasper report.

Below is a sample code, which illustrates the implementation of the XML generated from the Jasper Studio in the Spring boot application.

@Service
public class ReportService {
public String generateReport() {
try {
ArrayList <school> list = new ArrayList<>();
list.add(new school("1","h","2"));
String reportPath = "C:\\Users\\Jaspersoft\\MyReports";
// Compile the Jasper report from .jrxml to .japser
JasperReport jasperReport = JasperCompileManager.compileReport(reportPath + "\\Sample_A4.jrxml");
// Get your data source
JRBeanCollectionDataSource jrBeanCollectionDataSource = new JRBeanCollectionDataSource(list);
// Add parameters
Map<String, Object> parameters = new HashMap<>();
parameters.put("createdBy", "your string data");
// Fill the report
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,
jrBeanCollectionDataSource);
// Export the report to a PDF file
JasperExportManager.exportReportToPdfFile(jasperPrint, reportPath + "\Rpt.pdf");
System.out.println("Done");
return "Report successfully generated @path= " + reportPath;
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}

Happy learning!!

--

--

hariom sinha

Web Developer | 4 + Years of Experience | Loves CSS | ReactJs | Angular | React Native | Technical Blogger | Content Writer