I have following configuration:
@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
// objectMapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
return objectMapper;
}
and following dependencies:
ext {
springBootVersion = '1.5.2.RELEASE'
}
....
dependencies {
compile('org.springframework.boot:spring-boot-starter-websocket')
compile("org.springframework:spring-messaging")
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-validation')
compile('org.springframework.boot:spring-boot-starter-web')
compile group: 'net.jcip', name: 'jcip-annotations', version: '1.0'
compile ("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
testCompile('org.springframework.boot:spring-boot-starter-test')
}
I added the following controller:
@PostMapping("/validation_test")
public String testValidation(@Valid @RequestBody ClientInputMessage clientInputMessage, BindingResult result) {
logger.info(Arrays.toString(result.getAllErrors().toArray()));
return "main";
}
public class ClientInputMessage {
@NotEmpty
private String num1;
@NotEmpty
private String num2;
@Past
private LocalDateTime date;
If I pass json like this:
{
"num1":"324",
"num2":123,
"date":"2014-01-01"
}
application prints following output:
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize value of type java.time.LocalDateTime from String "2014-01-01": Text '2014-01-01' could not be parsed at index 10
at [Source: java.io.PushbackInputStream@1204f40f; line: 4, column: 8] (through reference chain: model.ClientInputMessage["date"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.time.LocalDateTime from String "2014-01-01": Text '2014-01-01' could not be parsed at index 10
at [Source: java.io.PushbackInputStream@1204f40f; line: 4, column: 8] (through reference chain: model.ClientInputMessage["date"])
To create a LocalDateTime object from a string you can use the static LocalDateTime. parse() method. It takes a string and a DateTimeFormatter as parameter. The DateTimeFormatter is used to specify the date/time pattern.
LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second. Other date and time fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. Time is represented to nanosecond precision.
You can get the time from the LocaldateTime object using the toLocalTime() method. Therefore, another way to get the current time is to retrieve the current LocaldateTime object using the of() method of the same class. From this object get the time using the toLocalTime() method.
Original answer:
LocalDateTime in java does not accept "2014-01-01" as a valid date string.
Some additional info:
If you don't actually care what type your date is (LocalDate, OffsetDate, ZonedDate, ...), you can make it a TemporalAccessor, then use DateTimeFormatter::parseBest to parse the date.
P.S.
string "2014-01-01T00:00:00" will be valid for LocalDateTime
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With