Home > Blockchain >  Is there any way to define a current DateTime as a default value for Date in @RequestBody of get met
Is there any way to define a current DateTime as a default value for Date in @RequestBody of get met

Time:01-06

here is my model class for the request body

@NoArgsConstructor
@AllArgsConstructor
@Data
public class ActiveAccountRequest {
    @Column(name = "startDate")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime startDate;
    @Column(name = "endDate")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endDate;
    private List<Character> channel;
}

Here is my Service class

@Service
public class ReportingGenService {
    @Autowired
    private ReportingGenRepo reportingGenRepo;

    public Page<ActiveAccountReport> paginatedActiveAccountReports(ActiveAccountRequest activeAccountRequest,
                                                   Integer page,Integer size) {

        Pageable pageable = PageRequest.of(page,size);
        Page<ActiveAccountReport> activeAccountReports  =  reportingGenRepo
          .getActiveAccountReportFilters(activeAccountRequest.getStartDate(),
                activeAccountRequest.getEndDate(),activeAccountRequest.getChannel(),pageable);

        return activeAccountReports;
    }


}

Here is my Controller class

@RestController
@RequestMapping("/repo")
public class ReportingGenController {
    @Autowired
    private ReportingGenService reportingGenService;

    @GetMapping("/get")
    public Page<ActiveAccountReport> findAll(@RequestBody ActiveAccountRequest activeAccountRequest,
                                   @RequestParam(value = "page",defaultValue = "0") Integer page, @RequestParam(value = "size",defaultValue = "1") Integer size){
        return reportingGenService.paginatedActiveAccountReports(activeAccountRequest,page,size);

    }
}

Now I want to give a default value for my endDate and startDate in the requestBody.Can anyone help me please.

CodePudding user response:

Just initialize the fields with desired default LocalDateTime.

CodePudding user response:

Yes you can

When you are going to declare your variables Stat date or end Date you can initialize with default values example

Public class RequestsBody Startdate= 10/12/2022; Enddate=10/12/2023;

Like that

  • Related