hibernate validator pojo field with ZoneDateTime field have none normal year

Viewed 63

In spring-boot application with rest api and hibernate validator.

Having an api with the following request body, the body can be received as long value or string value. in case a client sends the date as long value, the value can be in seconds or milliseconds.

@Value
@Builder(toBuilder = true)
public class DataObj {
  @Future
  ZonedDateTime myDate
}

When a client send the value as milliseconds, the zonedDatetime value can be with year over 54700 which then causes problems when saving in DB, and in any case year 54700 is not what the client ment to send. I am searching for a way to validate the value to have a 'normal year'

1 Answers

you can specify a pattern to use

@JsonSerialize(using = ZonedDateTimeSerializer.class)
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
ZonedDateTime myDate

and spring boot (starting form v 2) will parse the string to ZonedDateTime.

Related