public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime arg0, JsonGenerator arg1, SerializerProvider arg2) throws IOException, JsonProcessingException {
arg1.writeString(arg0.toString());
}
}
public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonParser arg0, DeserializationContext arg1) throws IOException, JsonProcessingException {
return LocalDateTime.parse(arg0.getText());
}
}
随机事实:如果我在类之上嵌套并且不使它们成为静态,则错误消息很奇怪: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
@Provider
public class JacksonObjectMapper implements ContextResolver<ObjectMapper> {
final ObjectMapper defaultObjectMapper;
public JacksonObjectMapper() {
defaultObjectMapper = createDefaultMapper();
}
@Override
public ObjectMapper getContext(Class<?> type) {
return defaultObjectMapper;
}
private static ObjectMapper createDefaultMapper() {
final ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
return mapper;
}
}
在您的资源中注册Jackson时,您需要像这样添加此映射器:
final ResourceConfig rc = new ResourceConfig().packages("<your package>");
rc
.register(JacksonObjectMapper.class)
.register(JacksonJaxbJsonProvider.class);
10 回答
这里不需要使用自定义序列化器/反序列化器 . 使用jackson-modules-java8's datetime module:
This module adds support for quite a few classes:
持续时间
瞬发
LocalDateTime
LocalDate
LocalTime
月日
OffsetDateTime
偏移时间
期间
年
YearMonth
ZonedDateTime
ZoneId
ZoneOffset
更新:出于历史原因留下这个答案,但我不推荐它 . 请参阅上面接受的答案 .
告诉Jackson使用您的自定义[de]序列化类进行映射:
提供自定义类:
随机事实:如果我在类之上嵌套并且不使它们成为静态,则错误消息很奇怪:
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
这个maven依赖将解决您的问题:
我一直在努力的一件事是,在反序列化期间,ZonedDateTime时区被更改为GMT . 事实证明,默认情况下, Jackson 用上下文中的一个替换它 . 为了保持区域1必须禁用此“功能”
如果您使用的是fastxml的ObjectMapper类,默认情况下ObjectMapper不了解LocalDateTime类,因此,您需要在gradle / maven中添加另一个依赖项:
现在,您需要将此库提供的数据类型支持注册到objectmapper对象,这可以通过以下方式完成:
现在,在您的jsonString中,您可以轻松地将java.LocalDateTime字段设置如下:
通过这一切,您的Json文件到Java对象转换将正常工作,您可以通过以下方式读取该文件:
使用 Spring boot 时遇到了类似的问题 . 使用Spring引导1.5.1.RELEASE我所要做的就是添加依赖:
如果您正在使用Jersey,那么您需要像其他人建议的那样添加Maven依赖项(jackson-datatype-jsr310)并注册您的对象映射器实例,如下所示:
在您的资源中注册Jackson时,您需要像这样添加此映射器:
如果您出于任何原因无法使用
jackson-modules-java8
,您可以使用@JsonIgnore
和@JsonGetter
&@JsonSetter
尽快(取消)序列化即时字段:例:
产量
这只是一个例子,如何在我讨厌调试此问题的单元测试中使用它 . 关键因素是
杰克森-datype-jsr310的
代码:import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.testng.Assert; import org.testng.annotations.Test;
我用这种时间格式:
"{birthDate": "2018-05-24T13:56:13Z}"
从json反序列化为java.time.Instant(见截图)您可以在
application.yml
文件中设置此项以解析即时时间,即java8中的Date API: