1

I have a SpringBoot project with two classes DashboardController.java and DashboardService.java. I need to define the variable that I get from the Controller to use it in the whole Service class. I explain the problem.

This is the method I have in the DashboardController.java, in which I collect by URL the variable iniDate from the front-end:

@GetMapping(path = { "/{employee_id}/{iniDate}" })
public EmployeeDashboardDto getEmployeeDashboarYearInidDto(
    @ApiParam(value = "employee_id", required = true) @PathVariable("employee_id") Integer idEmployee,
    @ApiParam(value = "iniDate", required = true) @PathVariable("iniDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate iniDate
) throws QOException {
    return dashboardService.getEmployeeDashboardYearIniDto(idEmployee,iniDate);
}

And this is the method that I have in the DashboardService.java class in which I collect the iniDate variable from the Controller:

public EmployeeDashboardDto getEmployeeDashboardYearIniDto(Integer idEmployee, EmployeeDashboardDto iniDate) {
    EmployeeDashboardDto initDate = iniDate;
    return initDate;
}

I'm not sure if I collect the variable correctly, but what I need first is to collect the variable from the front-end in the controller using the URL, then collect it in the service and finally define that variable is the service to use it in the rest of the methods.

myeongkil kim
  • 2,465
  • 4
  • 16
  • 22
sinfryd
  • 70
  • 7

2 Answers2

0

You are receiving the date in your controller and casting it as LocalDate then your method in the service class needs to receive the date in the same type LocalDate.

Change the parameter type to LocalDate like this:

public EmployeeDashboardDto getEmployeeDashboardYearIniDto(Integer idEmployee, LocalDate iniDate) {
    EmployeeDashboardDto employeeDashboardDto = new EmployeeDashboardDto();
    employeeDashboardDto.setIniDate(iniDate);
    return employeeDashboardDto;
}

If you need to receive dates as PathVariables you can check this or this answer.

David Jesus
  • 1,981
  • 2
  • 29
  • 34
0

I have created the following method in the DashboardService.java I have changed the name of the initDate variable:

public EmployeeDashboardDto getEmployeeDashboardYearIniDto(Integer employeeId, LocalDate iniDate) {
        EmployeeDashboardDto initDate = new EmployeeDashboardDto();
        initDate.setIniDate(iniDate);
        return initDate;
    }

And in the Dto I have created the following method:

public void setIniDate(LocalDate iniDate) {
        // TODO Auto-generated method stub      
    }

But in the Service, in the methods that is used the initDate variable says that initDate cannot be resolved to a variable

sinfryd
  • 70
  • 7
  • 1
    Maybe it's better if you update your question with more details and the full classes you have mentioned – David Jesus Feb 03 '21 at 08:29
  • The only thing I have added is the method to set in the DashboardDto.java and in the DashboardService.java I have added the method you said but changing the name of the variable to initDate, but in the rest of the ways I can't read that variable – sinfryd Feb 03 '21 at 08:35