我正在使用描述的the Jackson Datatype JSR310 page库,但是我仍然很难让它工作。
我已经配置了以下bean:
@Bean
@Primary
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JSR310Module());
return mapper;
}当我调用REST API时,输出的日期格式是yyyy-MM-dd'T'HH:ss.SSSSSS,例如2015-04-11T00:10:38.905847。我的AngularJS代码可以很好地处理这个问题。
当我想要向REST API提交一些东西时,日期是以yyyy-MM-dd'T'HH:mm:ss.SSS'Z'格式发布的,例如2015-04-09T08:30:00.000Z
杰克逊一直在抱怨结尾的“Z”。如果我查看文档中的LocalDateTimeDeserializer,它使用的是DateTimeFormatter.ISO_LOCAL_DATE_TIME,它会变成ISO_LOCAL_DATE'T'ISO_LOCAL_TIME,并且它提到它没有重写区。
所以我想我应该在我创建的ObjectMapper上设置DateFormat:
@Bean
@Primary
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JSR310Module());
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
return mapper;
}但这没有任何作用。我将其更改为简单的格式,如yyyy-MM-dd,但序列化的日期仍然是以前的格式,反序列化也不受影响。
为了让它正常工作,我做错了什么?据我所知,我的JavaScript代码中的日期格式是ISO8601格式...
发布于 2015-11-05 22:10:55
没有必要编写自己的序列化程序。使用默认格式就足够了,但是用另一种格式( time_zone格式)创建一个实例,这样超出的部分就会被剪切掉:
new LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME)在我的例子中,我有一个这样的contextResolver需要在配置级别上实现:
@Service
@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
private final ObjectMapper mapper;
public ObjectMapperContextResolver() {
mapper = new ObjectMapper();
JavaTimeModule javaTimeModule=new JavaTimeModule();
// Hack time module to allow 'Z' at the end of string (i.e. javascript json's)
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME));
mapper.registerModule(javaTimeModule);
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
}
@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
}发布于 2015-04-12 00:33:28
目前,LocalDateTimeDeserializer似乎没有考虑为对象映射器设置的日期格式。
要使其工作,您可以覆盖LocalDateTimeDeserializer或切换到使用ZoneDateTime,它处理最后的“Z”字符。
下面是一个示例:
public class Java8DateFormat {
public static void main(String[] args) throws IOException {
final ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JSR310Module());
// mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
final String date = mapper.writeValueAsString(new Date());
System.out.println(date);
System.out.println(mapper.readValue(date, ZonedDateTime.class));
}
}输出:
"2015-04-11T18:24:47.815Z"
2015-04-11T18:24:47.815Z[GMT]发布于 2017-07-28 22:17:22
Hibernate 4,Spring 4- REST WS,Client - Spring Boot 1.5.2。在我的例子中,我在Entity ZonedDateTime类中使用来映射数据库中的时间戳。Hibernate和Spring Boot REST运行良好。我只需将库添加到pom文件中:
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jsr310 -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>因此,我认为,由于well.The jackson.version是最新的,所以该转换器是在LocalDateTime的Spring中实现的。
https://stackoverflow.com/questions/29571587
复制相似问题