java - 在Spring Boot中的requestmapping和restcontroller中发送开始和结束日期

标签 java hibernate spring-boot jpa

我正在使用 hibernate jpa 从 Spring Boot 中的多个表中获取数据。 我的查询很完美并且工作正常,我只是陷入了如何从休息 Controller 传递开始和日期并调用 URL 的困境。 这是我的休息 Controller 代码:

@RequestMapping("/employeeMonthlyAttendance/startDate/{startDate}/endDate/{endDate}")
public String generateEmployeeMonthlyAttendanceReport(HttpServletResponse response, @PathVariable Date startDate, @PathVariable Date endDate){

    try {
        System.out.println("METHOD CALLED");

        List<EmployeeMonthlyAttendanceReport> employees = employeeMonthlyAttendanceReportService.getEmployeeMonthlyAttendance(startDate, endDate) ;

        // Get your data source
        JRBeanCollectionDataSource jrBeanCollectionDataSource = new JRBeanCollectionDataSource(employees);

        // Add parameters
//            Map<String, Object> parameters = new HashMap<>();


        JasperPrint jasperPrint = null;

        //For Download PDF File
//            response.setContentType("application/x-download");
//            response.setHeader("Content-Disposition",   String.format("attachment; filename=\"All Sections Employees.pdf\""));

        //For  Direct View PDF FILE
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", String.format("inline; filename=\"Employees Monthly Attendance Report.pdf\""));

        OutputStream out = response.getOutputStream();
        jasperPrint = jasperReportService.exportPDFFileWithData("EmployeeMonthlyAttendanceReport", new HashedMap(), jrBeanCollectionDataSource);
        JasperExportManager.exportReportToPdfStream(jasperPrint, out);

        System.out.println("Done");

        return "Report successfully generated";
    } catch (Exception e) {
        e.printStackTrace();
        return "Error--> check the console log";
    }


}

最佳答案

@PathVariable("startDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate @PathVariable("endDate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate

尝试在generateEmployeeMonthlyAttendanceReport() 方法中进行这些更改。希望这对您有帮助。

关于java - 在Spring Boot中的requestmapping和restcontroller中发送开始和结束日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58485656/

相关文章:

java - é 未正确解析

hibernate - 使用 Gorm 查询列子集

spring-boot - 在 spring boot 中从 application.properties 获取属性

json - 如何使用 Spring Boot 在 Mongo 中存储原始 JSON

java - 针对未经身份验证的用户的 Spring Security 404 页面

java - 扩展应用程序的扩展抛出 InvocationTargetException

java - 如何在 JSpinner 中禁用值的舍入

java - 这个分隔符怎么做呢?

java - Vaadin 7 : Filters, EntityManager 每个请求( hibernate )、JPAContainer 和推送/websockets 使用

spring - 在Gradle中进行集成测试时未找到持久类