Sping PDF REST API with JasperReports
Java, Maven, JasperReports, REST, Spring Boot
JasperReports is a Java class library, and it is meant for those Java developers who need to add reporting capabilities to their applications.
Introduction
In this post, we will create a PDF report that is exposed over a RESTful endpoint using Spring Boot. This is also a good place to start for those of you trying to figure out how to integrate JasperReports with Spring Framework 5. To serve excel over REST, refer to this Excel Post.
Prerequisites
Project Structure
At the end of this guide our folder structure will look similar to the following:
.
|__src/
| |__main/
| | |__java/
| | | |__com/
| | | | |__juliuskrah/
| | | | | |__jasper/
| | | | | | |__Application.java
| | | | | | |__ApplicationProperties.java
| | | | | | |__report/
| | | | | | | |__JasperReportsService.java
| | | | | | | |__ReportService.java
| | | | | | |__storage/
| | | | | | | |__FileSystemStorageService.java
| | | | | | | |__StorageException.java
| | | | | | | |__StorageFileNotFoundException.java
| | | | | | | |__StorageService.java
| | | | | | |__web/
| | | | | | | |__rest/
| | | | | | | | |__ApplicationResource.java
| | |__resources/
| | | |__reports/
| | | | |__pdf_rest_resource.jrxml
| | | | |__pdf_rest.jrxml
| | | |__application.yaml
| | | |__cherry.png
| | | |__logo.png
|__pom.xml
How to complete this guide
To complete this guide, download (zip| tar.gz) and unzip the source repository for this guide.
Create PDF Report
Let us start by implementing a solution to generate PDFs in JasperReports:
file:
src/main/java/com/juliuskrah/jasper/report/JasperReportsService.java
Render the PDF REST Endpoint
We will create the REST endpoint:
file:
src/main/java/com/juliuskrah/jasper/web/rest/ApplicationResource.java
Now navigate to http://localhost:8080/:username to view your report.
Conclusion
In this post we learned how to generate PDF with JasperReports and serve it over HTTP with Spring Boot.
As usual you can find the full example to this guide in the github repository. Until the next post, keep doing cool
things .