You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为GET请求@RequestBody中的日期字段设置默认值?

解决方案

方法一:修改请求体模型类,在无参构造中设置默认值

Spring反序列化请求体时,会先调用无参构造创建对象,再为有传入值的字段赋值。在无参构造里初始化日期字段为当前时间,若请求中传入对应值,会自动覆盖默认值。

修改ActiveAccountRequest类:

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

    // 重写无参构造,设置默认值
    public ActiveAccountRequest() {
        // 如需指定时区(如东八区),可替换为 LocalDateTime.now(ZoneId.of("Asia/Shanghai"))
        this.startDate = LocalDateTime.now();
        this.endDate = LocalDateTime.now();
    }
}

方法二:在控制器层处理默认值

如果不想修改模型类,可在控制器接收请求后,判断字段是否为空,为空则赋值当前时间,这种方式更灵活,方便后续调整默认值逻辑。

修改ReportingGenController的findAll方法:

@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){
        // 处理startDate默认值
        if(activeAccountRequest.getStartDate() == null){
            activeAccountRequest.setStartDate(LocalDateTime.now());
        }
        // 处理endDate默认值
        if(activeAccountRequest.getEndDate() == null){
            activeAccountRequest.setEndDate(LocalDateTime.now());
        }
        return reportingGenService.paginatedActiveAccountReports(activeAccountRequest,page,size);
    }
}

额外提示:GET请求使用@RequestBody的规范问题

REST规范中,GET请求通常不携带请求体(技术上支持,但部分HTTP客户端或网关可能存在兼容性问题),更推荐将参数作为@RequestParam传递。若可调整接口设计,建议修改为:

@GetMapping("/get")
public Page<ActiveAccountReport> findAll(
        @RequestParam(value = "startDate", required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime startDate,
        @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") LocalDateTime endDate,
        @RequestParam(value = "channel", required = false) List<Character> channel,
        @RequestParam(value = "page",defaultValue = "0") Integer page, 
        @RequestParam(value = "size",defaultValue = "1") Integer size){
    // 赋值默认值
    if(startDate == null) startDate = LocalDateTime.now();
    if(endDate == null) endDate = LocalDateTime.now();
    ActiveAccountRequest request = new ActiveAccountRequest(startDate, endDate, channel);
    return reportingGenService.paginatedActiveAccountReports(request,page,size);
}

内容的提问来源于stack exchange,提问作者Imran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 23:05:17