默认情况下,Jackson 将ZonedDateTime
字段值写入为数值。
要将 Jackson 配置为将它们写入 ISO-8601 兼容字符串,请WRITE_DATES_AS_TIMESTAMPS
通过JsonFactoryDecorator
...禁用序列化功能
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.MappingJsonFactory;
import com.fasterxml.jackson.databind.SerializationFeature;
import net.logstash.logback.decorate.JsonFactoryDecorator;
public class TimestampJsonFactoryDecorator implements JsonFactoryDecorator {
public JsonFactory decorate(JsonFactory factory) {
((MappingJsonFactory) factory).getCodec()
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return factory;
}
}
然后配置编码器以使用该装饰器...
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<jsonFactoryDecorator class="your.TimestampJsonFactoryDecorator"/>
</encoder>
另请参阅自定义 JSON 工厂和生成器