Spring Boot 有没有办法在get方法的@RequestBody中将当前DateTime定义为Date的默认值

rqdpfwrv  于 2023-01-09  发布在  Spring
关注(0)|答案(3)|浏览(238)

下面是请求主体的模型类

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

用于JPA连接的我的存储库用户

@Repository
public interface ReportingGenRepo extends JpaRepository<ReportingGeneral, Integer> {

    @Query(value = "SELECT REPORTING_GENERAL.USERNAME AS userName, " +
            "ANY_VALUE (REPORTING_GENERAL.CONTACT_NUMBER ) AS contactNumber,ANY_VALUE (REPORTING_GENERAL.PRIMARY_KEY) AS primaryKey," +
            "MIN( REQUEST_TIME ) AS minRequestTime ,MAX( REQUEST_TIME ) AS maxRequestTime, " +
            "COUNT(IF ( RESPONSE_CODE = '1', 1, NULL )) AS success,COUNT(IF " +
            "( RESPONSE_CODE != '1', 1, NULL )) AS failed,COUNT(*) AS totalHits,CHANNEL as channel" +
            " FROM REPORTING_GENERAL WHERE " +
            " REPORTING_GENERAL.ID > 0 AND CHANNEL IN ?3 AND (REPORTING_GENERAL.REQUEST_TIME  BETWEEN ?1 AND ?2)" +
            "GROUP BY channel, username",
            countQuery = "SELECT count(*) FROM REPORTING_GENERAL WHERE " +
                    " REPORTING_GENERAL.ID > 0 AND CHANNEL IN ?3 AND (REPORTING_GENERAL.REQUEST_TIME  BETWEEN ?1 AND ?2)" +
                    "GROUP BY channel, username ", nativeQuery = true)
    public Page<ActiveAccountReport> getActiveAccountReportFilters(
            LocalDateTime startDate,
            LocalDateTime endDate,
            List<String> channel,
            Pageable pageable);

下面是我的Controller类

@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);

    }
}

现在我想在requestBody中为我的endDate和startDate给予一个默认值。

h4cxqtbf

h4cxqtbf1#

只需使用所需的默认值LocalDateTime初始化字段。

alen0pnh

alen0pnh2#

当使用Lombok尤其是@Data注解时,您无法设置默认值,
在我看来,你有两个选择来设置你的变量的默认数据,
第一个:从类中删除@Data注解,实现getter和setter手册并设置值,如以下代码片段所示

@NoArgsConstructor
@AllArgsConstructor
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 = LocalDateTime.now();
    @Column(name = "endDate")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endDate = LocalDateTime.now();
    private List<Character> channel;

//getter and setter
}

对于第二个,您可以使用内部类构建器来实现它,请查看此引用https://www.baeldung.com/lombok-builder-default-value

wlzqhblo

wlzqhblo3#

你可以的
当你要声明你的变量Stat date或end Date时,你可以用默认值初始化,例如

LocalDateTime aDateTime = LocalDateTime.of(2015, 
                                       Month.JULY, 29, 19, 30, 40);

像这样

相关问题