Spring Boot Jasper Report Example (2024)
Overview
In this tutorial, we'll create spring boot application with Jasper Report and generate PDF report.
Install the Jasper Studio to create the report view and utilize in the spring boot application.
Q: What is JasperReports?
Ans:
JasperReports is a Java reporting tool that can output to a wide range of destinations, including the screen, a printer, PDF, HTML, Microsoft Excel, RTF, ODT, comma-separated values, and XML files. It can be used to generate dynamic content in Java-enabled applications, such as Java EE or web applications.
Let's create Spring Boot Project from Spring Initializer site https://start.spring.io/
Project Structure
Maven Dependency
The jasperreports
dependency must be added in the pom.xml
to use
the Jasper Report features.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.techgeeknext</groupId>
<artifactId>Spring-Boot-Jasper-Report</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring-Boot-Jasper-Report</name>
<description>Spring Boot Jasper Report Example</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.16.0</version>
</dependency>
</dependencies>
<repositories>
<!-- JasperSoft, they modified a standard library for their own special needs -->
<repository>
<id>jaspersoft-third-party</id>
<url>https://jaspersoft.jfrog.io/jaspersoft/third-party-ce-artifacts/</url>
</repository>
</repositories>
<pluginRepositories>
<!-- JasperSoft, they modified a standard library for their own special needs -->
<pluginRepository>
<id>jaspersoft-third-party</id>
<url>https://jaspersoft.jfrog.io/jaspersoft/third-party-ce-artifacts/</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Take a look at our suggested posts:
Jasper Report Template
Copy the created employees-details.jrxml
report in resource folder of spring boot
application from the Jasper Studio Report .
Model
Create Employee Model to map the data with employees-details.jrxml
report.
package com.techgeeknext.model;
public class Employee {
private int id;
private String name;
private String role;
private String address;
public Employee(int id, String name, String role, String address) {
this.id = id;
this.name = name;
this.role = role;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Rest Controller
Create a rest endpoint to generate the report and pass in the dynamic values that the report requires. The report will be generated in PDF format. We can generate the report in the format specified by our requirements.
package com.techgeeknext.controller;
import com.techgeeknext.model.Employee;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class EmployeeController {
@GetMapping("/employee/records/report")
public ResponseEntity<byte[]> getEmployeeRecordReport() {
try {
// create employee data
Employee emp1 = new Employee(1, "AAA", "BBB", "A city");
Employee emp2 = new Employee(2, "XXX", "ZZZ", "B city");
List<Employee> empLst = new ArrayList<Employee>();
empLst.add(emp1);
empLst.add(emp2);
//dynamic parameters required for report
Map<String, Object> empParams = new HashMap<String, Object>();
empParams.put("CompanyName", "TechGeekNext");
empParams.put("employeeData", new JRBeanCollectionDataSource(empLst));
JasperPrint empReport =
JasperFillManager.fillReport
(
JasperCompileManager.compileReport(
ResourceUtils.getFile("classpath:employees-details.jrxml")
.getAbsolutePath()) // path of the jasper report
, empParams // dynamic parameters
, new JREmptyDataSource()
);
HttpHeaders headers = new HttpHeaders();
//set the PDF format
headers.setContentType(MediaType.APPLICATION_PDF);
headers.setContentDispositionFormData("filename", "employees-details.pdf");
//create the report in PDF format
return new ResponseEntity<byte[]>
(JasperExportManager.exportReportToPdf(empReport), headers, HttpStatus.OK);
} catch(Exception e) {
return new ResponseEntity<byte[]>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
Test WebClient
Follow below steps to generate the Jasper Report (Spring Boot Jasper Report PDF Example).
-
Run Spring Boot + Jasper Report Example by using
mvn spring-boot run
command. - Open Postman use employees report rest endpoint http://localhost:8080/employee/records/report and click on Save Response -> Save to a file to save the file at your location.
- It will generate the PDF file called
employees-details.pdf
at your selected location. Once you open it'll show the generated data.
Download Source Code
The full source code for this article can be found on below.Download it here - Spring Boot Jasper Report Example