Spring Boot web JasperReports integration (2024)

Table of Contents
The CARS table Application FAQs

last modified July 13, 2020

In this tutorial, we show how to use JasperReports with Spring Boot framework. We createa web application.

JasperReports is a Java open source reporting library. It can create reports invarious formats including PDF, HTML, XLS, or CSV. JasperReports creates page-oriented,ready-to-print documents in a simple and flexible manner.

JdbcTemplate is a Spring library that helps programmers createapplications that work with relational databases and JDBC. It takes care of manytedious and error-prone low-level details such as handling transactions,cleaning up resources, and correctly handling exceptions.JdbcTemplate is shipped in Spring's spring-jdbc module.

Spring is a Java application framework for developing Javaenterprise applications. It also helps integrate various enterprise components.Spring Boot makes it easy to create Spring-powered, production-grade applicationsand services with minimum setup requirements.

Apache Derby is an open source relational database implemented entirely in Java.It has small footprint and is easy to deploy and install. It can be run inembedded and client/server modes.

The CARS table

We use the following table:

cars.sql

-- SQL for the CARS tableCREATE TABLE CARS(ID BIGINT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), NAME VARCHAR(30), PRICE INT);INSERT INTO CARS(Name, Price) VALUES('Audi', 52642);INSERT INTO CARS(Name, Price) VALUES('Mercedes', 57127);INSERT INTO CARS(Name, Price) VALUES('Skoda', 9000);INSERT INTO CARS(Name, Price) VALUES('Volvo', 29000);INSERT INTO CARS(Name, Price) VALUES('Bentley', 350000);INSERT INTO CARS(Name, Price) VALUES('Citroen', 21000);INSERT INTO CARS(Name, Price) VALUES('Hummer', 41400);INSERT INTO CARS(Name, Price) VALUES('Volkswagen', 21600);

The cars.sql file creates the CARS table.

$ $DERBY_HOME/bin/ijij version 10.11ij> CONNECT 'jdbc:derby:testdb';ij> RUN 'cars.sql';

One option is to use the ij tool to create the table from the SQL script.Refer to Apache Derby tutorial to familiarize yourselfwith Derby.

$ $DERBY_HOME/bin/NetworkServerControl start &

Derby server is started with NetworkServerControl tool.

Application

The following Spring Boot application loads data from a database tableand produces a PDF report from it with JasperReports library. The applicationruns with embedded Tomcat server.

$ tree.├── pom.xml└── src ├── main │ ├── java │ │ └── com │ │ └── zetcode │ │ ├── Application.java │ │ ├── bean │ │ │ └── Car.java │ │ ├── conf │ │ │ ├── AppConfig.java │ │ │ └── MvcConf.java │ │ ├── controller │ │ │ └── MyController.java │ │ └── service │ │ ├── CarService.java │ │ └── ICarService.java │ └── resources │ ├── application.yml │ ├── report2.jrxml │ └── static │ └── index.html └── test └── java

This is the project structure.

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>JasperSpringBootWeb</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.apache.derby</groupId> <artifactId>derbyclient</artifactId> <version>10.13.1.1</version> </dependency> <dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>6.4.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>

The Maven pom.xml file contains dependencies for JasperReports library,Derby driver, and Spring Boot.

report2.xml

<?xml version = "1.0" encoding = "UTF-8"?><!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"><jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"> <field name="Id" class="java.lang.Long"> <fieldDescription><![CDATA[id]]></fieldDescription> </field> <field name="Name" class="java.lang.String"> <fieldDescription><![CDATA[name]]></fieldDescription> </field> <field name="Price" class="java.lang.Integer"> <fieldDescription><![CDATA[price]]></fieldDescription> </field> <detail> <band height="15"> <textField> <reportElement x="0" y="0" width="50" height="15" /> <textElement textAlignment="Right" verticalAlignment="Middle"/> <textFieldExpression class="java.lang.Long"> <![CDATA[$F{Id}]]> </textFieldExpression> </textField> <textField> <reportElement x="150" y="0" width="100" height="15" /> <textElement textAlignment="Left" verticalAlignment="Middle"/> <textFieldExpression class="java.lang.String"> <![CDATA[$F{Name}]]> </textFieldExpression> </textField> <textField> <reportElement x="200" y="0" width="100" height="15"/> <textElement textAlignment="Right" verticalAlignment="Middle"/> <textFieldExpression class="java.lang.Integer"> <![CDATA[$F{Price}]]> </textFieldExpression> </textField> </band> </detail></jasperReport>

This is the report template file. It is located in the src/main/resources directory.The template contains only the detail band. Inside a detail band, each element is repeated forevery record provided by the data source.

<field name="Id" class="java.lang.Long"> <fieldDescription><![CDATA[id]]></fieldDescription></field><field name="Name" class="java.lang.String"> <fieldDescription><![CDATA[name]]></fieldDescription></field><field name="Price" class="java.lang.Integer"> <fieldDescription><![CDATA[price]]></fieldDescription></field>

We have three fields in the report. The fields are mapped to the elements ofthe data source beans.

<textField> <reportElement x="0" y="0" width="50" height="15" /> <textElement textAlignment="Right" verticalAlignment="Middle"/> <textFieldExpression class="java.lang.Long"> <![CDATA[$F{Id}]]> </textFieldExpression></textField>

A text field is an element that is filled with dynamic data.We place a value from a field inside the text field. We refer to thevariable with the $F{} syntax.

com/zetcode/Car.java

package com.zetcode.bean;public class Car { private Long id; private String name; private int price; public Car() {} public Car(Long id, String name, int price) { this.id = id; this.name = name; this.price = price; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public String toString() { return "Car{" + "id=" + id + ", name=" + name + ", price=" + price + '}'; }}

This is Car bean class. It contains item ID, name, and price.

application.yml

datasource: url: jdbc:derby://localhost:1527/testdb username: app password: app driverClassName: org.apache.derby.jdbc.ClientDriver

The application.yml is the main Spring Boot configurationfile. It contains the Derby datasource.

com/zetcode/AppConfig.java

package com.zetcode.conf;import javax.sql.DataSource;import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;@Configurationpublic class AppConfig { @Bean @Primary @ConfigurationProperties(prefix = "datasource") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); }}

The AppConfig is a Java configuration class. It createsthe data source bean from the configuration file.

com/zetcode/MyController.java

package com.zetcode.controller;import com.zetcode.service.ICarService;import java.util.HashMap;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView;@Controllerpublic class MyController { @Autowired private ApplicationContext appContext; @Autowired private ICarService carService; @RequestMapping(path = "/pdf", method = RequestMethod.GET) public ModelAndView report() { JasperReportsPdfView view = new JasperReportsPdfView(); view.setUrl("classpath:report2.jrxml"); view.setApplicationContext(appContext); Map<String, Object> params = new HashMap<>(); params.put("datasource", carService.findAll()); return new ModelAndView(view, params); }}

In the MyController, we have two methods that react to tworequests.

@Autowiredprivate ICarService carService;

We inject CarService object into the attribute. The serviceobject is used to retrieve data from the database.

@RequestMapping(path = "/pdf", method = RequestMethod.GET)public ModelAndView report() { JasperReportsPdfView view = new JasperReportsPdfView(); view.setUrl("classpath:report2.jrxml"); view.setApplicationContext(appContext); Map<String, Object> params = new HashMap<>(); params.put("datasource", carService.getCars()); return new ModelAndView(view, params);}

In the report method, a report is generated and sentback to the client. The JasperReportsPdfView is a Springclass that generates a PDF report from the provided template and data.

com/zetcode/ICarService.java

package com.zetcode.service;import com.zetcode.bean.Car;import java.util.List;public interface ICarService { public List<Car> findAll();}

ICarService provides a contract method toget all cars from the data source.

com/zetcode/CarService.java

package com.zetcode.service;import com.zetcode.bean.Car;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.BeanPropertyRowMapper;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Service;@Servicepublic class CarService implements ICarService { @Autowired private JdbcTemplate jtm; @Override public List<Car> findAll() { String sql = "SELECT * FROM Cars"; List<Car> cars = jtm.query(sql, new BeanPropertyRowMapper(Car.class)); return cars; }}

CarService contains the implementation ofthe findAll method. We retrieve all cars fromthe CARS table with the help of the JdbcTemplate.

@Autowiredprivate JdbcTemplate jtm;

JdbcTemplate is injected.

String sql = "SELECT * FROM Cars";

This is SQL to be executed. We select all cars from the CARS table.

List<Car> cars = jtm.query(sql, new BeanPropertyRowMapper(Car.class));

BeanPropertyRowMapper converts a row into a new instance of thespecified mapped target class.

index.html

<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>Home Page</title> </head> <body> <a href="/pdf.html">Generate report</a> </body></html>

The index.html file contains a link to generate the PDF report.

com/zetcode/Application.java

package com.zetcode;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}

The Application sets up the Spring Boot application.

In this tutorial, we have created a PDF report with JasperReports. Theapplication used Spring Boot framework and was run in a web environment.

Spring Boot web JasperReports integration (2024)

FAQs

How to integrate Jasper report with Java? ›

Procedure
  1. Step 1: Create a Java Application. Create a Java with Ant Application, Provide a Project Name and make sure to uncheck Create Main Class.
  2. Step: 2: Add a Button. I added a button and renamed the text to “Run Report”
  3. Step 3: Add JAR file references. Add JAR file references to the application. ...
  4. Step 6: Run report.
Mar 30, 2018

What is the use of Jasper in Spring Boot? ›

JasperReports is a Java reporting tool that writes content to the printer, onto the screen, or into files like PDF, XLS, RTF, CSV, XML, HTML, ODT, and TXT. Combined with Spring Boot, we can create APIs allowing users to download the application data in the form of reports.

How to generate reports using Spring Boot? ›

First, you need to create a REST endpoint in your Spring Boot application. You can create a controller class with a method to generate the CSV report that takes in any parameters that you need for your CSV report. In the above code, we have created a REST endpoint with a GET request mapping /api/generate-csv-report .

What is Jasper in Java? ›

JasperReports is an open source Java reporting tool that can write to a variety of targets, such as: screen, a printer, into PDF, HTML, Microsoft Excel, RTF, ODT, comma-separated values (CSV), XSL, or XML files.

How to read Jasper file in Java? ›

Viewing Reports. JasperReport provides a built-in viewer for viewing the generated reports in its original format. It is a swing based component and other Java applications can integrate this component without having to export the documents to other formats in order to be viewed or printed.

What is the difference between Jasper and Jrxml? ›

jrxml is a human readable XML file that contains the report template i.e. report structure and its formatting rules. . jasper is the compiled report template i.e. compiled . jrxml file.

What is jasper in web application? ›

Jaspersoft is the customizable reporting and analytics platform you need to succeed. We've spent over two decades perfecting our platform so you can deliver the data visualizations and analytics your customers want, from high volumes of pixel perfect reports to self-service ad hoc reports and more.

How to add dynamic image in Jasper report using Java? ›

Just directly select the bytea column as in select image, id ... , set the field type to java. awt. Image and use $F{image} as image expression.

Why use Lombok in Spring Boot? ›

It lets developers use annotations and then generates the boilerplate code after the code is compiled. Lombok uses annotation processing to achieve this functionality. The annotation processing was introduced in Java 5 and it is very useful to generate additional source files during the compilation phase.

How do I get a list of records in Spring Boot? ›

Run the application

Once server is up and running, Use Postman to make a POST request to add a record first. Set the following parameters in POSTMAN. Click on Send Button and check the response status to be OK. Now make a GET Request to get all records.

How does Jasperreport work? ›

JasperReports Server lets you analyze data using chart visualizations or tabular views against predefined Topics, Domains, or OLAP connections. The Ad Hoc report designer is the primary tool in JasperReports Server for analyzing data.

Why do we need Jasper? ›

A large language model (LLM) called Jasper AI may be used to generate text, translate languages, write various types of creative material, and provide you with helpful answers to your queries. It can be used for many things, including customer support, email marketing, copywriting, and content creation.

Is JasperReports free? ›

Jaspersoft's Community releases are freely available. Everyone is free to download the binary distributions, source code, samples, and documentation that accompany the Community release software. However, it does not contain the additional functionality found in commercial editions.

How do I add JasperReports to Intellij? ›

Installation & Usage

Open Settings > Plugins > Marketplace in your Intellij Idea IDE, search for Jasper Report Support and install the plugin.

How to add dynamic image in JasperReport using Java? ›

Just directly select the bytea column as in select image, id ... , set the field type to java. awt. Image and use $F{image} as image expression.

How do I import a JasperReport? ›

Import or Export Data Through the UI
  1. Log into Standalone as sysadmin.
  2. Click Manage > Server Settings > Import.
  3. Select the data you wish to import, then click Import.

How do I connect to JasperReport server? ›

Connection to JasperReports Server
  1. Name: the name of the connection. You can use any name you want. ...
  2. User: the username to access the server. The default for the local server is "jasperadmin".
  3. Password: as with the username, for the local server by default it is "jasperadmin".
Jan 9, 2024

Top Articles
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 5645

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.