Home > database >  Spring and JasperReport: java.io.FileNotFoundException: class path resource [reportes/index.html.jas
Spring and JasperReport: java.io.FileNotFoundException: class path resource [reportes/index.html.jas

Time:11-27

I'm developing a web app with Spring and JasperReports. I compiled the report in JasperSoft Studio and copied the jasper file (ListadoAlumno.jasper) to my Spring projects and created a controller for the report (ReporteController.java). My project has an index.html page which is the one that loads automatically when I use http://localhost:8080/ in my internet browser. The problem is that since I added the ReporteController.java I get the following error:

java.io.FileNotFoundException: class path resource [reportes/index.html.jasper] cannot be opened because it does not exist

I think the problem is due to the following code in the controller ClassPathResource resource = new ClassPathResource("reportes" File.separator reportName ".jasper");. If I directly enter the name of the report in the browser (http://localhost:8080/ListadoAlumno) it loads with no problem. The problem is when trying to load the index.html page.

See below the full code of the ReporteController.Java and the structure of my project.

@RestController
public class ReporteController {
@Autowired
private DataSource dataSource;

@RequestMapping("/{reportName}")
public void demoReport1(@PathVariable("reportName") final String reportName,
        @RequestParam(required = false) Map<String, Object> parameters, HttpServletResponse response,
        HttpServletRequest request) throws Exception {
    parameters = parameters == null ? new HashMap<>() : parameters;
    ClassPathResource resource = new ClassPathResource("reportes"   File.separator   reportName   ".jasper");
    InputStream jasperStream = resource.getInputStream();
    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());
    response.setContentType("application/pdf");
    response.setHeader("Content-Disposition", "inline;");
    final OutputStream outputStream = response.getOutputStream();
    JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
  }
}

enter image description here

CodePudding user response:

Found the solution, changed the RequesteMapping to @RequestMapping("/{reportName}") so the controller is not invoked when the index.html page is loaded.

  • Related