ich folgende Konfiguration haben:Wert kann nicht vom Typ java.time.LocalDateTime von String deserialisieren
@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;
}
und folgende Abhängigkeiten:
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')
}
ich folgende Controller hinzugefügt:
@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;
Wenn ich json so überlasse:
{
"num1":"324",
"num2":123,
"date":"2014-01-01"
}
Anwendung druckt folgende Ausgabe:
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: [email protected]; 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: [email protected]; line: 4, column: 8] (through reference chain: model.ClientInputMessage["date"])
Versuchen Sie "Datum": "2014-01-01T00: 00: 00". LocalDateTime in Java akzeptiert "2014-01-01" nicht als gültige Datumszeichenfolge. –
@Jure Kolenko OOPS, es funktioniert – gstackoverflow
Verwenden Sie für einen Nur-Datum-Wert wie "2014-01-01" die Klasse "LocalDate" und nicht die Klasse "LocalDateTime". Wenn Ihr Problem gelöst ist, dann sollten entweder Sie oder Kolenko eine Antwort posten, die akzeptiert wird, um diese Frage zu schließen. –