ARTICLE AD BOX
Since your ad-hoc deserializers are instantiated reflectively by Jackson, does it mean your custom deserializers can't do the "dependency injection" thing, declare dependencies in its constructor?
Also, are there ways to "autowire" such dependencies with Spring?
@JsonDeserialize(using=DateTimeFormatDeserializer.class) public class DateTimeFormatDeserializer extends JsonDeserializer<Date>{ // no declared constructor, implicit no-args only @Override public Date deserialize(JsonParser jp, DeserializationContext ctxt)Let me emphasize, deserializers are not global but instead declared on an ad-hoc basis:
@JsonProperty("Evn_setDT") // weirdly named property coming from external API @JsonSerialize(using = DateTimeFormatSerializer.class) @JsonDeserialize(using = DateTimeFormatDeserializer.class) private Date evnSetDT;I assume (correct me if I'm wrong) it means declaring a bean like this
@Bean public Jackson2ObjectMapperBuilder jacksonBuilder() { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); builder.deserializerByType(Date.class, new DateTimeFormatDeserializer(myService)); // Register the deserializer bean return builder; }is not an option.
I'd like to inject supported date formats in the deserializer so that such formats could be listed in a property file and changed, if necessary, without rebuilding the project (or injected directly in a unit test).
Java 8, Jackson 2.13.2.2, Spring 5.3.18.
