Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在spring引导中,在rest控制器的requestmapping中发送开始和结束日期_Java_Hibernate_Spring Boot_Jpa - Fatal编程技术网

Java 在spring引导中,在rest控制器的requestmapping中发送开始和结束日期

Java 在spring引导中,在rest控制器的requestmapping中发送开始和结束日期,java,hibernate,spring-boot,jpa,Java,Hibernate,Spring Boot,Jpa,我使用hibernate jpa从spring boot中的多个表中获取数据。 我的查询很完美,工作也很好,我只是被困在如何从rest控制器传递开始和日期以及调用URL中。 以下是我的rest控制器代码: @RequestMapping("/employeeMonthlyAttendance/startDate/{startDate}/endDate/{endDate}") public String generateEmployeeMonthlyAttendanceReport(HttpSer

我使用hibernate jpa从spring boot中的多个表中获取数据。 我的查询很完美,工作也很好,我只是被困在如何从rest控制器传递开始和日期以及调用URL中。 以下是我的rest控制器代码:

@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";
    }


}
@RequestMapping(“/employeeMonthlyAttendance/startDate/{startDate}/endDate/{endDate}”)
公共字符串generateEmployeeMonthlyAttendanceReport(HttpServletResponse,@PathVariable Date startDate,@PathVariable Date endDate){
试一试{
System.out.println(“调用的方法”);
List employeeMonthlyAttendanceReportService.getEmployeeMonthlyAttendance(开始日期,结束日期);
//获取您的数据源
JRBeanCollectionDataSource JRBeanCollectionDataSource=新的JRBeanCollectionDataSource(员工);
//添加参数
//映射参数=新的HashMap();
JasperPrint JasperPrint=null;
//下载PDF文件
//setContentType(“应用程序/x下载”);
//response.setHeader(“内容处置”,String.format(“附件;文件名=\”所有部分Employees.pdf\”);
//用于直接查看PDF文件
response.setContentType(“application/pdf”);
response.setHeader(“Content Disposition”,String.format(“inline;filename=\“Employees Monthly Attention Report.pdf\”);
OutputStream out=response.getOutputStream();
jasperPrint=jasperReportService.ExportPffielWithData(“EmployeeMonthlyAttendanceReport”,new HashedMap(),jrBeanCollectionDataSource);
JasperExportManager.exportReportToPdfStream(jasperPrint,out);
系统输出打印项次(“完成”);
返回“报表生成成功”;
}捕获(例外e){
e、 printStackTrace();
返回“错误-->检查控制台日志”;
}
}
@PathVariable(“startDate”)@DateTimeFormat(pattern=“yyyy-MM-dd”) 日期startDate@PathVariable(“endDate”)@DateTimeFormat(模式= “yyyy-MM-dd”)日期结束日期


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

首先,您需要通过在rest控制器的导入部分添加导入org.springframework.format.annotation.DateTimeFormat在方法参数中添加DateTimeFormat

您的控制器方法如下所示:

    @RequestMapping("/employeeMonthlyAttendance/startDate/{startDate}/endDate/{endDate}")
        public String generateEmployeeMonthlyAttendanceReport(HttpServletResponse response, 
        @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate, @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate){
        }
现在,您可以通过传递格式化日期从任何REST客户端调用端点

您的URL如下所示:


我希望这就是您想要的

您能分享您希望使用的数据格式吗?例如:dd-MM-yyyyyyyyyyy-MM-dd这将是日期的格式,我的理解是您希望将此REST端点称为
/employeeMonthlyAttendance/startDate/{startDate}/endDate/{endDate}
,并且需要一种格式来传入{startDate}和{endDate},对吗?@Safer Ansari否我想在rest控制器中使用日期参数调用url您要调用哪个url,调用url所需的代码在哪里?