Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

Your platform library defines an ObjectMapper bean marked @Primary, and application teams keep fighting it. How would you redesign how the library provides defaults?

A @Primary ObjectMapper in a library takes over injection for every consumer and leaves application teams no clean way to define their own. I would remove the @Primary bean and provide defaults through auto-configuration with @ConditionalOnMissingBean, so an application's own ObjectMapper replaces the library's. Better still, the library contributes customiser beans, such as Jackson builder customisers and modules, so teams adjust Spring Boot's ObjectMapper instead of replacing it. Then we document the extension points and test overriding in the library's own tests.

Why this question is asked

ObjectMapper is the most fought-over bean in many Spring code bases, because JSON settings touch every API, every message and every cache. When a platform library forces its own mapper with @Primary, teams start defining mappers with odd names, copying settings, or excluding the library entirely. Interviewers ask this design question to see whether we understand how a library should share defaults without taking control away from the application. In this answer, we will recap why @Primary in a library causes the fight, then redesign step by step: back-off with auto-configuration, customisers instead of replacement, and the documentation and tests that keep it working.

1Why the @Primary Bean Causes the Fight

@Primary marks the bean that wins whenever several beans of a type exist. Inside one application that is a clear, local choice. Inside a library it means every consumer gets the library's ObjectMapper for every plain injection point, including Spring MVC's message converters and any other library that asks for an ObjectMapper.

Java
// platform-lib
@Configuration
class PlatformJsonConfig {
@Bean @Primary
ObjectMapper platformObjectMapper() {
return new ObjectMapper().registerModule(new JavaTimeModule())
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
}
}
// application team
@Bean @Primary ObjectMapper ordersMapper() { ... }
// NoUniqueBeanDefinitionException: more than one 'primary' bean found among candidates
// and without @Primary, their mapper is simply ignored by Spring MVC

Notice that the team has no good move. A second @Primary fails startup. A non-primary bean is ignored everywhere they did not add a qualifier. Excluding the library's configuration also removes everything else it provides. And because the library built its mapper with new ObjectMapper(), it also threw away Spring Boot's own Jackson setup, such as spring.jackson.* properties, so those properties silently stopped working too.

2Back Off With @ConditionalOnMissingBean

The first fix is to make the library's bean a default rather than a decision. We move it into an auto-configuration class, a configuration that Spring Boot loads after the application's own configuration, listed in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports, and guard it with @ConditionalOnMissingBean.

Java
@AutoConfiguration(before = JacksonAutoConfiguration.class)
class PlatformJsonAutoConfiguration {
@Bean
@ConditionalOnMissingBean(ObjectMapper.class) // the app's own mapper wins, no @Primary needed
ObjectMapper platformObjectMapper(Jackson2ObjectMapperBuilder builder) {
return builder.build(); // still honours spring.jackson.* properties
}
}
// App defines no ObjectMapper -> platformObjectMapper
// App defines ordersMapper -> ordersMapper, the platform one is never created

Notice that the library's mapper is now built from Spring Boot's builder, so spring.jackson.* properties work again. Because auto-configuration runs after user configuration, @ConditionalOnMissingBean sees the application's bean and steps aside. This is a big improvement, but it still makes teams choose between all of the platform's settings or none of them.

3Customise Instead of Replace

What teams usually want is not a different mapper but one or two different settings. Spring Boot already supports this: it builds the ObjectMapper from a builder and applies every customiser bean it finds, and it registers every Jackson Module bean automatically. So the library can stop defining an ObjectMapper at all and contribute only its settings.

Java
@AutoConfiguration(before = JacksonAutoConfiguration.class)
class PlatformJsonAutoConfiguration {
@Bean
@Order(-1) // before Boot property customiser (0) and app ones
Jackson2ObjectMapperBuilderCustomizer platformJsonDefaults() {
return b -> b.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.serializationInclusion(JsonInclude.Include.NON_NULL);
}
@Bean Module platformMoneyModule() { return new MoneyModule(); } // registered by Boot automatically
}
// application team: adjust one thing, keep the rest
@Bean Jackson2ObjectMapperBuilderCustomizer ordersJson() {
return b -> b.serializationInclusion(JsonInclude.Include.ALWAYS); // overrides the platform's NON_NULL
}
Step throughHow one ObjectMapper gets built
Spring BootPlatform libraryApplicationObjectMapper
create builder from spring.jackson.* properties
apply platform customiser (Order -1)
apply application customisers (later order)
register all Module beans
build the single ObjectMapper
injected everywhere, including MVC converters

6 messages in order. Play them, or step through them one at a time.

Everyone adjusts the same mapper; nobody replaces it.

Now there is exactly one ObjectMapper, built by Spring Boot, and everybody contributes to it. The platform's defaults apply first, then Boot's property-based customiser, then the application's customisers, which run last and win on conflicts. The names above are the Spring Boot 3 Jackson 2 classes; Spring Boot 4 moves to Jackson 3, with JsonMapper and its own builder customiser (check your version for the exact names), but the design is the same.

4Document and Test the Extension Points

An extension point that is not documented will not be used, and one that is not tested will break. The library's README should say plainly: the platform does not define an ObjectMapper; it contributes these settings and modules; to change a setting, add a customiser bean or a spring.jackson property; to opt out of a setting, override it in your customiser; to replace everything, define your own ObjectMapper, and here is what you lose.

Then we test each promise with ApplicationContextRunner, which starts a small context with the library's auto-configuration and lets us add user configuration.

Java
private final ApplicationContextRunner runner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(JacksonAutoConfiguration.class, PlatformJsonAutoConfiguration.class));
@Test void appCustomiserWinsOverPlatformDefault() {
runner.withBean(Jackson2ObjectMapperBuilderCustomizer.class,
() -> b -> b.serializationInclusion(JsonInclude.Include.ALWAYS))
.run(ctx -> assertThat(ctx.getBean(ObjectMapper.class)
.writeValueAsString(new Dto(null))).isEqualTo("{\"name\":null}")); // passes
}
@Test void appMapperReplacesEverything() {
runner.withBean("custom", ObjectMapper.class, ObjectMapper::new)
.run(ctx -> assertThat(ctx).hasSingleBean(ObjectMapper.class).hasBean("custom")); // passes
}

These tests are the library's contract. If someone on the platform team re-adds @Primary in a year, the second test fails before any application team feels it.

5Follow-up questions

What if some platform code really needs a mapper with fixed settings?

Then that mapper is private to the platform and should not compete for the ObjectMapper type at all. The library can create it internally without exposing it as a bean, or register it under a custom qualifier such as @PlatformInternal and inject it only with that qualifier. Application code never sees it, and the application's main mapper stays under the application's control.

How do we roll this out without breaking teams that depend on today's behaviour?

Release the change in a minor version with the old behaviour kept behind a property, for example platform.json.legacy-primary=true, and a startup warning when it is used. Publish migration notes showing the customiser equivalent of each old setting. In the next major version, remove the legacy path.

Why order the platform customiser before the application's?

Customisers are applied in order, and later ones can override earlier settings. Spring Boot's own customiser, which applies the spring.jackson.* properties, has order 0. Giving the platform customiser order -1 makes its settings the base layer, so spring.jackson.* properties and any application customiser, which by default has the lowest precedence and runs last, can override them. The reverse would let the platform silently undo the application's choices.

6Conclusion

In this answer, we saw that a @Primary ObjectMapper in a library takes over every injection point and leaves teams with no clean way to change JSON behaviour. The redesign first moves the bean into auto-configuration with @ConditionalOnMissingBean, so the application's own mapper replaces it, and then goes further by dropping the library's mapper and contributing customisers and modules to Spring Boot's single ObjectMapper, which teams can adjust rather than replace. Documenting those extension points and testing them with ApplicationContextRunner keeps the contract from regressing.

What to say in the interviewAbout 90 seconds aloud

Open with

The root problem is that @Primary in a library takes a decision that belongs to the application. Every plain injection point, including Spring MVC's converters, gets the library's mapper, a team's second @Primary fails startup, and a non-primary bean is ignored.

Then, if they want more

And because the library builds it with new ObjectMapper, spring.jackson properties stop working too. I would redesign in two steps. First, move the bean into an auto-configuration class with @ConditionalOnMissingBean and build it from Spring Boot's builder. Auto-configuration runs after the application's own configuration, so any ObjectMapper a team defines simply replaces the platform one, with no @Primary anywhere.

Second, and better, stop defining an ObjectMapper in the library at all. Spring Boot already builds one from a builder, applies every builder customiser bean in order and registers every Jackson Module bean.

So the platform contributes a customiser with a low order for its defaults, plus its modules, and teams add their own customisers that run later and win on conflicts. There is then one mapper that everyone adjusts. In Boot 4 with Jackson 3 the class names change but the design is the same.

Finally, I would document exactly how to change a setting, opt out, or replace the mapper, and lock those promises with ApplicationContextRunner tests in the library, so nobody can quietly reintroduce @Primary later.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3, Spring Boot 4
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Your platform library defines an ObjectMapper bean marked @Primary, and application...": https://scrivio-interview-library.netlify.app/spring-boot/dependency-injection/9d482de1b3/