Add configuration property for Jackson's DateTimeFeature

See gh-45535
This commit is contained in:
Andy Wilkinson 2025-09-09 17:58:16 +01:00
parent ee87246974
commit c86eb5298a
4 changed files with 37 additions and 0 deletions

View File

@ -70,6 +70,10 @@ These features are described in several enums (in Jackson) that map onto propert
| `spring.jackson.datatype.json-node.<feature_name>` | `spring.jackson.datatype.json-node.<feature_name>`
| `true`, `false` | `true`, `false`
| javadoc:tools.jackson.databind.DateTimeFeature[]
| `spring.jackson.datetime.<feature_name>`
| `true`, `false`
| javadoc:tools.jackson.databind.DeserializationFeature[] | javadoc:tools.jackson.databind.DeserializationFeature[]
| `spring.jackson.deserialization.<feature_name>` | `spring.jackson.deserialization.<feature_name>`
| `true`, `false` | `true`, `false`

View File

@ -175,6 +175,7 @@ public final class JacksonAutoConfiguration {
configureFeatures(builder, this.jacksonProperties.getMapper(), builder::configure); configureFeatures(builder, this.jacksonProperties.getMapper(), builder::configure);
configureFeatures(builder, this.jacksonProperties.getRead(), builder::configure); configureFeatures(builder, this.jacksonProperties.getRead(), builder::configure);
configureFeatures(builder, this.jacksonProperties.getWrite(), builder::configure); configureFeatures(builder, this.jacksonProperties.getWrite(), builder::configure);
configureFeatures(builder, this.jacksonProperties.getDatetime(), builder::configure);
configureFeatures(builder, this.jacksonProperties.getDatatype().getEnum(), builder::configure); configureFeatures(builder, this.jacksonProperties.getDatatype().getEnum(), builder::configure);
configureFeatures(builder, this.jacksonProperties.getDatatype().getJsonNode(), builder::configure); configureFeatures(builder, this.jacksonProperties.getDatatype().getJsonNode(), builder::configure);
configureDateFormat(builder); configureDateFormat(builder);

View File

@ -30,6 +30,7 @@ import tools.jackson.core.json.JsonWriteFeature;
import tools.jackson.databind.DeserializationFeature; import tools.jackson.databind.DeserializationFeature;
import tools.jackson.databind.MapperFeature; import tools.jackson.databind.MapperFeature;
import tools.jackson.databind.SerializationFeature; import tools.jackson.databind.SerializationFeature;
import tools.jackson.databind.cfg.DateTimeFeature;
import tools.jackson.databind.cfg.EnumFeature; import tools.jackson.databind.cfg.EnumFeature;
import tools.jackson.databind.cfg.JsonNodeFeature; import tools.jackson.databind.cfg.JsonNodeFeature;
@ -90,6 +91,11 @@ public class JacksonProperties {
*/ */
private final Map<JsonWriteFeature, Boolean> write = new EnumMap<>(JsonWriteFeature.class); private final Map<JsonWriteFeature, Boolean> write = new EnumMap<>(JsonWriteFeature.class);
/**
* Jackson on/off features for DateTime processing.
*/
private final Map<DateTimeFeature, Boolean> datetime = new EnumMap<>(DateTimeFeature.class);
/** /**
* Controls the inclusion of properties during serialization. Configured with one of * Controls the inclusion of properties during serialization. Configured with one of
* the values in Jackson's JsonInclude.Include enumeration. * the values in Jackson's JsonInclude.Include enumeration.
@ -160,6 +166,10 @@ public class JacksonProperties {
return this.write; return this.write;
} }
public Map<DateTimeFeature, Boolean> getDatetime() {
return this.datetime;
}
public JsonInclude.@Nullable Include getDefaultPropertyInclusion() { public JsonInclude.@Nullable Include getDefaultPropertyInclusion() {
return this.defaultPropertyInclusion; return this.defaultPropertyInclusion;
} }

View File

@ -41,6 +41,7 @@ import tools.jackson.databind.SerializationFeature;
import tools.jackson.databind.ValueSerializer; import tools.jackson.databind.ValueSerializer;
import tools.jackson.databind.cfg.ConstructorDetector; import tools.jackson.databind.cfg.ConstructorDetector;
import tools.jackson.databind.cfg.ConstructorDetector.SingleArgConstructor; import tools.jackson.databind.cfg.ConstructorDetector.SingleArgConstructor;
import tools.jackson.databind.cfg.DateTimeFeature;
import tools.jackson.databind.cfg.EnumFeature; import tools.jackson.databind.cfg.EnumFeature;
import tools.jackson.databind.cfg.JsonNodeFeature; import tools.jackson.databind.cfg.JsonNodeFeature;
import tools.jackson.databind.exc.InvalidFormatException; import tools.jackson.databind.exc.InvalidFormatException;
@ -262,6 +263,27 @@ class JacksonAutoConfigurationTests {
}); });
} }
@Test
void enableDatetimeFeature() {
this.contextRunner.withPropertyValues("spring.jackson.datetime.write-dates-as-timestamps:true")
.run((context) -> {
JsonMapper mapper = context.getBean(JsonMapper.class);
DateTimeFeature feature = DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS;
assertThat(feature.enabledByDefault()).isFalse();
assertThat(mapper.isEnabled(feature)).isTrue();
});
}
@Test
void disableDatetimeFeature() {
this.contextRunner.withPropertyValues("spring.jackson.datetime.adjust-dates-to-context-time-zone:false")
.run((context) -> {
JsonMapper mapper = context.getBean(JsonMapper.class);
assertThat(DateTimeFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE.enabledByDefault()).isTrue();
assertThat(mapper.isEnabled(DateTimeFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)).isFalse();
});
}
@Test @Test
void enableEnumFeature() { void enableEnumFeature() {
this.contextRunner.withPropertyValues("spring.jackson.datatype.enum.write-enums-to-lowercase=true") this.contextRunner.withPropertyValues("spring.jackson.datatype.enum.write-enums-to-lowercase=true")