【发布时间】:2017-01-23 13:02:01
【问题描述】:
我正在使用 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 仍然会中断。
【问题讨论】:
标签: spring date spring-boot jackson jodatime