将日期字符串映射到 Spring 中的日期,包括 Jackson 的时间
Mapping Date Strings to Dates including Time with Jackson in Spring
我正在使用 Spring Boot 1.4 和以下工作。我有这个 @Bean
定义:
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
mapper.registerModule(new JodaModule());
return new MappingJackson2HttpMessageConverter(mapper);
}
我定义了这个 DTO:
public class ReportRequest implements Serializable {
private LocalDate startDate;
private LocalDate endDate;
// additional fields and getters/setters omitted
}
我将此数据提交到带有 @RequestBody ReportRequest
的控制器中,请求正文中包含以下 json:
{
"startDate": "2016-09-01",
"endDate": "2016-09-12"
}
效果很好。但是,我还需要包括时间。所以我将所有内容更改为如下所示:
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
private LocalDateTime startDate;
private LocalDateTime endDate;
{
"startDate": "2016-09-01 02:00:00",
"endDate": "2016-09-12 23:59:59"
}
这不起作用。我得到:
Could not read document: Invalid format: \"2016-09-01 02:00:00\" is malformed at \" 02:00:00\" (through reference chain: com.hightouchinc.dto.ReportRequest[\"startDate\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid format: \"2016-09-01 02:00:00\" is malformed at \" 02:00:00\" (through reference chain: com.hightouchinc.dto.ReportRequest[\"startDate\"])
更新:我修改了以下内容:
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"));
并且可以发送 "2016-09-01T02:00:00"
并且有效。但是从两者中删除 T
继续中断。
LocalDateTimeDeserializer
似乎不尊重传递给 setDateFormat()
的值,如 JodaModule
:
中所示
addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
您可以做的是在注册之前覆盖模块中的默认反序列化器:
JodaModule jodaModule = new JodaModule();
JacksonJodaDateFormat format = new JacksonJodaDateFormat("yyyy-MM-dd HH:mm:ss");
jodaModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(format)));
mapper.registerModule(jodaModule);
并且应该使用正确的模式反序列化您的 LocalDateTime
个实例。
我正在使用 Spring Boot 1.4 和以下工作。我有这个 @Bean
定义:
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
mapper.registerModule(new JodaModule());
return new MappingJackson2HttpMessageConverter(mapper);
}
我定义了这个 DTO:
public class ReportRequest implements Serializable {
private LocalDate startDate;
private LocalDate endDate;
// additional fields and getters/setters omitted
}
我将此数据提交到带有 @RequestBody ReportRequest
的控制器中,请求正文中包含以下 json:
{
"startDate": "2016-09-01",
"endDate": "2016-09-12"
}
效果很好。但是,我还需要包括时间。所以我将所有内容更改为如下所示:
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
private LocalDateTime startDate;
private LocalDateTime endDate;
{
"startDate": "2016-09-01 02:00:00",
"endDate": "2016-09-12 23:59:59"
}
这不起作用。我得到:
Could not read document: Invalid format: \"2016-09-01 02:00:00\" is malformed at \" 02:00:00\" (through reference chain: com.hightouchinc.dto.ReportRequest[\"startDate\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid format: \"2016-09-01 02:00:00\" is malformed at \" 02:00:00\" (through reference chain: com.hightouchinc.dto.ReportRequest[\"startDate\"])
更新:我修改了以下内容:
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"));
并且可以发送 "2016-09-01T02:00:00"
并且有效。但是从两者中删除 T
继续中断。
LocalDateTimeDeserializer
似乎不尊重传递给 setDateFormat()
的值,如 JodaModule
:
addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
您可以做的是在注册之前覆盖模块中的默认反序列化器:
JodaModule jodaModule = new JodaModule();
JacksonJodaDateFormat format = new JacksonJodaDateFormat("yyyy-MM-dd HH:mm:ss");
jodaModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(format)));
mapper.registerModule(jodaModule);
并且应该使用正确的模式反序列化您的 LocalDateTime
个实例。