public class ZonedDateTimeToMillisecondSerializer extends JsonSerializer<ZonedDateTime> {
@Override
public void serialize(ZonedDateTime value,
JsonGenerator jsonGenerator,
SerializerProvider provider)
throws IOException {
jsonGenerator.writeNumber(value.toInstant().toEpochMilli());
}
}
需要在实体类字段上加上注解@JsonSerialize(using= ZonedDateTimeToMillisecondSerializer.class)
public class TimeConvert implements Converter<ZonedDateTime, Long> {
@Override
public Long convert(ZonedDateTime value) {
long l = value.toInstant().toEpochMilli();
return l;
}
@Override
public JavaType getInputType(TypeFactory typeFactory) {
return typeFactory.constructType(new TypeReference<ZonedDateTime>() {});
}
@Override
public JavaType getOutputType(TypeFactory typeFactory) {
return typeFactory.constructType(new TypeReference<Long>() {});
}
}
需要在实体类字段上加上注解@JsonSerialize(converter= TimeConvert.class)