Serializing Java-8 LocalDateTime Using DateTimeFormatter

I am using Dropwizard 0.8.4 and jackson-datatype-jsr310.

a) I would like to serialize my LocalDateTime output to JSON as DateTimeFormatter.ISO_INSTANT, but could not find any clean way to do this (without implementing custom serialization classes), should this not be very standard for simple annotations?

My code currently works with:

@JsonProperty
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss'Z'")
@JsonSerialize(using = LocalDateTimeSerializer.class)
public LocalDateTime getTime() {
    ...
}

but this template is not quite the same as ISO_INSTANT, and since I debugged it a bit, ISO_INSTANT cannot even be represented as a string template.

b) Can I better use Joda-Time, which is supported by Dropwizard by default?

c) Is there a way to skip serializing a Java field into JSON based on the value (boolean false)? I tried @JsonFilter and SimpleBeanPropertyFilter but didn't get it to work, and it seems to be outdated too. Also @JsonProperty (defaultValue ...) doesn't work either.

+4
source share

Source: https://habr.com/ru/post/1618105/


All Articles