Skip to content

Commit

Permalink
Fix deps .
Browse files Browse the repository at this point in the history
added embeded db for running directly also bumped boot version .
  • Loading branch information
gauravbrills committed Jul 22, 2021
1 parent 2584ac0 commit 6648b16
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 47 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ A demo for using jasper within a Spring Boot App I made for a Jasper session.

This depicts integration of jasper reports within a spring boot app .

To run do `mvn spring-boot:run`

Mostly uses concept of JasperReportsViewResolver as defined here http://stackoverflow.com/questions/27532446/how-to-use-jasperreports-with-spring-mvc
[Check the answer with the tick there :smiley:]

Also the report included using my custom db and data so try putting your own .jrxml report there before trying to run.
# Update
As per Spring 5 https://github.com/spring-projects/spring-framework/issues/17884 JasperReportsViewResolver has been deprecated
hence updating code to use jasper directly .Will add an example soon .


`The jasper endpoints shall be like http://localhost:8080/report/{reportname}?format={format}&id={id} (id param are optional,you can add ur custom report params there)`
`The jasper endpoints shall be like http://localhost:8080/reports/{reportname}?format={format}&id={id} (id param are optional,you can add ur custom report params there)`
eg. `http://localhost:8080/reports/rpt_A?format=pdf`


Cheers! :wine_glass:
Expand Down
9 changes: 5 additions & 4 deletions jasperreportswithboot/config/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
spring.datasource.url=jdbc:mysql://localhost/pumpkinhotel
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
20 changes: 7 additions & 13 deletions jasperreportswithboot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.8.RELEASE</version>
<version>1.5.17.RELEASE</version>
</parent>
<groupId>gauravbrills.demo</groupId>
<artifactId>jasperreportswithboot</artifactId>
Expand Down Expand Up @@ -39,16 +39,6 @@
<artifactId>lombok</artifactId>
<version>1.14.4</version><!--$NO-MVN-MAN-VER$-->
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.0.3-1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
Expand Down Expand Up @@ -96,7 +86,11 @@
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<!-- Jasper Report Specific Jars END -->
<dependency>
Expand All @@ -112,7 +106,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- <configuration> <jvmArguments> -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y
<!-- <configuration> <jvmArguments> -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y
</jvmArguments> </configuration> -->
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
/**
*
*/
package gauravbrills.demo.jasper.web;

import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;
import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
Expand All @@ -17,6 +8,9 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.sql.DataSource;
import javax.websocket.server.PathParam;

/**
* @author grawat
*/
Expand All @@ -30,21 +24,14 @@ public class JasperEndPoint {
@Autowired
private DataSource dbsoruce;

/*
* @RequestMapping(value = "{reportname}", method = RequestMethod.GET) public ModelAndView getReportsFull(final ModelMap
* modelMap, ModelAndView modelAndView, @PathParam("reportname") final String reportname, @RequestParam(FILE_FORMAT) final
* String format) { // JRDataSource datasource = new JRBeanCollectionDataSource(dataMap); modelMap.put(DATASOURCE, dbsoruce);
* modelMap.put(FILE_FORMAT, format); modelAndView = new ModelAndView(reportname, modelMap); return modelAndView; }
*/

@RequestMapping(value = "{reportname}", method = RequestMethod.GET)
public ModelAndView getRptByParam(final ModelMap modelMap, ModelAndView modelAndView, @PathParam("reportname")
final String reportname, @RequestParam(FILE_FORMAT)
final String format, @RequestParam("id")
final String id) {

List<String> paramMap = new ArrayList<>();
paramMap.add(id);
// connecting to mysql
public ModelAndView getRptByParam(final ModelMap modelMap, ModelAndView modelAndView,
@PathParam("reportname") final String reportname,
@RequestParam(value = FILE_FORMAT, defaultValue = "pdf") final String format,
@RequestParam(value = "id", defaultValue = "%") final String id) {

// connecting to embedded h2
modelMap.put(DATASOURCE, dbsoruce);
modelMap.put(FILE_FORMAT, format);
modelMap.put("productid", id);
Expand Down
15 changes: 15 additions & 0 deletions jasperreportswithboot/src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
INSERT INTO m_product_master VALUES (1, 'Beach Deal');

INSERT INTO m_product_master VALUES (2, 'Paradise');

INSERT INTO m_product_master VALUES (3, 'Lonely Planet');

INSERT INTO m_product_master VALUES (4, 'Safari');

INSERT INTO m_hotel VALUES (1, 'Beach Deal', 'Beach Front', 51.507351, -0.127758, 4);

INSERT INTO m_hotel VALUES (2, 'Paradise', 'Somehwere in Swiss Alps', 51.507351, -0.127758, 5);

INSERT INTO m_hotel VALUES (3, 'Lonely Planet', 'Bermuda', 51.507351, -0.127758, 4);

INSERT INTO m_hotel VALUES (4, 'Safari', 'Cape Cod', 51.507351, -0.127758, 4);
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
<scriptlet name="CustomScriplet" class="gauravbrills.demo.jasper.reports.LogScriplet"/>
<parameter name="productid" class="java.lang.String"/>
<queryString language="SQL">
<![CDATA[select master.product_id,product_name,description,address,latitude,longitude,COALESCE (star_Rating,'0') rating from m_product_master master ,m_hotel hotel
where master.product_id = hotel.product_id and star_Rating <> "boutique" and
master.product_id like if( $P{productid} = "ALL","%", $P{productid})
order by rating desc]]>
<![CDATA[select master.product_id,product_name,description,address,latitude,longitude,COALESCE (star_Rating,'0') rating
from m_product_master master ,m_hotel hotel
where master.product_id = hotel.product_id and master.product_id like $P{productid} order by rating desc]]>
</queryString>
<field name="product_name" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
Expand Down
16 changes: 16 additions & 0 deletions jasperreportswithboot/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE TABLE m_product_master
(
product_id int NOT NULL PRIMARY KEY,
product_name varchar(255),
);


CREATE TABLE m_hotel
(
product_id int NOT NULL PRIMARY KEY,
description varchar(255),
address varchar(255),
latitude double,
longitude double,
star_Rating varchar(255),
);

0 comments on commit 6648b16

Please sign in to comment.