pom.xml
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>${jasperreports.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>batik</groupId>
<artifactId>batik-bridge</artifactId>
<version>1.6-1</version>
</dependency>
WebConfig.java
@Bean // Jasper preview bean Set.
public JasperReportsViewResolver getJasperReportsViewResolver() {
final JasperReportsViewResolver resolver = new JasperReportsViewResolver();
resolver.setPrefix("/resources/jasper/");
resolver.setSuffix(".jrxml");
resolver.setReportDataKey("datasource");
resolver.setViewNames("report_*"); // .jrxml file name & call name
resolver.setViewClass(JasperReportsMultiFormatView.class);
resolver.setOrder(0);
return resolver;
}
//preview sample code
@RequestMapping(value = "test-report", method = RequestMethod.GET)
public String companyList(final Model model) {
try {
Gson gson = new Gson();
testModel tt = new testModel();
tt.setTitle("Hello");
tt.setBarcode("HY12000");
tt.setUsers(sserService.findAllUsers());
InputStream is = new ByteArrayInputStream(gson.toJson(tt).getBytes());
JsonDataSource dataSource = new JsonDataSource(is, "");
model.addAttribute("datasource", dataSource);
model.addAttribute("format", "pdf"); // preview type
return "report_testreoprt"; // return .jrxml file name
} catch (JRException e) {
e.printStackTrace();
return "report_testreoprt"; // return .jrxml file name
}
}
//file Output code.
@Override
public void testPDFDownload(final String jsonData) throws JRException, IOException {
JasperDesign jasperDesign;
JasperPrint jasperPrint;
Resource resource = new ClassPathXmlApplicationContext().getResource("classpath:static/jasper/tesy.jrxml");
InputStream is = new ByteArrayInputStream(jsonData.getBytes());
jasperDesign = JRXmlLoader.load(resource.getInputStream());
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JsonDataSource dataSource = new JsonDataSource(is, "");
jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(),
dataSource);
//exportType
JasperExportManager.exportReportToPdfFile(jasperPrint,
"src/main/resources/static/jasper/report.pdf");
}