首页 > 解决方案 > Spring Boot 从请求中解析无效日期

问题描述

我需要在 Spring Boot(或 Jackson)上下文中解决这个问题。

不转换无效日期:31.2.2018 -> 28.2.2018 但抛出异常。

在这种情况下,我想避免为日期编写自定义序列化程序/格式化程序,并使用开箱即用的解决方案(例如 Jackson 注释或注释属性)。有这样的吗?

代码:

控制器有端点:

@PostMapping(value = "/endpoint")
public Events getAllEvents(@RequestBody EventRequest eventRequest) {}

EventRequest DTO 看起来像这样:

public class EventRequest {
    private LocalDateTime eventDate;

    @JsonCreator
    public EventRequest(@JsonProperty(...) @JsonFormat(pattern=...) eventDate){
        this.eventDate = eventDate; // 31.2.2018
        // Don't want to 31.2.2018 -> 28.2.2018 
        // but to THROW EXCEPTION
    }
}

标签: javadatetimespring-bootserializationjackson

解决方案


推荐阅读