简体   繁体   中英

Send start and end date in requestmapping in rest controller in spring boot

i am fetching data from multiple tables in spring boot using hibernate jpa. My query is perfect and working fine, I am just stuck in how to pass start and date from rest controller and call in URL. Here is my rest controller code:

@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

Try making these changes in generateEmployeeMonthlyAttendanceReport() method. Hope this would help you.

First of all, you need to add DateTimeFormat in your method parameter by adding import org.springframework.format.annotation.DateTimeFormat in the imports section of your rest controller.

Your controller method would look like:

    @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){
        }

Now you can call your endpoint from any REST client by passing a formatted date.

Your URL would look like: http://localhost:9999/reports/employees/employeeMonthlyAttendance/startDate/2019-07-01/endDate/2019-07-31

I hope this is what you're looking for

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM