Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you extend behavior of the spring boot autoconfiguration?

Tags:

spring-boot

I am looking to extend JacksonAutoConfiguration specifically when the builder is created, i would like to set that ObjectMapper to a Util class which has static setter for the ObjectMapper. Look at the line before returning builder where I would like to set ObjectMpper to a static class.

@Bean
        @ConditionalOnMissingBean(Jackson2ObjectMapperBuilder.class)
        public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder() {
            Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
            builder.applicationContext(this.applicationContext);
            if (this.jacksonProperties.getSerializationInclusion() != null) {
                builder.serializationInclusion(this.jacksonProperties
                        .getSerializationInclusion());
            }
            configureFeatures(builder, this.jacksonProperties.getDeserialization());
            configureFeatures(builder, this.jacksonProperties.getSerialization());
            configureFeatures(builder, this.jacksonProperties.getMapper());
            configureFeatures(builder, this.jacksonProperties.getParser());
            configureFeatures(builder, this.jacksonProperties.getGenerator());
            configureDateFormat(builder);
            configurePropertyNamingStrategy(builder);
            configureModules(builder);
            **ObjectMapperUtils.setObjectMapper( builder.build() );**
            return builder;
        }
like image 924
user3399267 Avatar asked Dec 30 '25 10:12

user3399267


1 Answers

The ObjectMapper created from the auto-configured Jackson2ObjectMapperBuilder is exposed as a bean in the JacksonAutoConfiguration. You could simply create another @Configuration class, get a reference to the ObjectMapper (via auto-wiring) and use an @PostConstruct method to set the ObjectMapper in your ObjectMapperUtils class.

Another suggestion would be to refactor ObjectMapperUtils so that it is created as a Spring bean itself, then you can auto-wire ObjectMapper directly into it.

like image 65
SteveO Avatar answered Jan 09 '26 15:01

SteveO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!