Skip to content

Add application property to customize Jackson default leniency #27547

@mjustin

Description

@mjustin

Jackson provides a leniency setting for deserialization, which can be configured per property (using @JsonFormat(lenient=OptBoolean.FALSE)) or globally (per ObjectMapper.setDefaultLeniency(false)).

This flag will become more useful as of (the not yet released) Jackson 2.13 per FasterXML/jackson-modules-java8#212, as disabling it will cause Jackson to reject dates with time components (e.g. "2007-12-03T10:15:30", "2007-12-03T10:15:30Z") when deserializing a LocalDate value. Disallowing these time components can be useful, as including them can lead to unintended issues truncating the time part results in the wrong date due to time zone discrepancies between the source & target systems.

In light of this, it would be handy if there were a way to set the default value of this lenient property through a Spring Boot application property.

Workaround

Right now, this property can be configured using a custom Jackson2ObjectMapperBuilderCustomizer bean. Since Spring's Jackson2ObjectMapperBuilder type does not directly expose this property, a post-configurer is probably the simplest way to set this:

@Configuration
public class CustomizedJacksonConfiguration {
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer nonLenientObjectMapperBuilderCustomizer() {
        return builder -> builder.postConfigurer(
                objectMapper -> objectMapper.setDefaultLeniency(false));
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions