我的实体中定义了java8 LocalDateTime参数。 我已经在wildfly 10中部署了我的jax-rs webapp。
当我进行REST调用时,我得到以下异常;
Caused by: java.time.format.DateTimeParseException: Text '2016-06-03T00:38:48.043Z' could not be parsed, unparsed text found at index 23
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1952)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:63)
at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:36)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95)
at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285)
... 56 more
我在ObjectMapper中注册了JavaTimeModule;
@Provider
public class LocalDateTimeConverterProvider implements ContextResolver<ObjectMapper>, ParamConverterProvider {
private final LocalDateTimeConverter converter = new LocalDateTimeConverter();
private final ObjectMapper mapper;
public LocalDateTimeConverterProvider() {
mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.registerModule(new JavaTimeModule());
}
@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
为什么我会遇到异常? Java8 JavaTimeModule提供的默认序列化器不起作用吗?
答案 0 :(得分:0)
使用ISO_DATE_TIME格式注册本地日期时间deserilizer,以允许本地日期时间接受时区。
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME)