Is it possible to register Controller specific ObjectMapper in SpringBoot
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring Boot makes JSON handling feel automatic because it wires a global Jackson ObjectMapper into the MVC stack. That default is convenient, but it also raises a practical question: what if one controller needs different JSON rules from the rest of the application?
Short Answer
Yes, but not in the sense of attaching a private mapper to a controller annotation and letting Spring magically switch behavior. Spring MVC selects HttpMessageConverter instances at the application level. To get controller-specific behavior, you usually choose one of three patterns:
- serialize explicitly with a secondary mapper,
- register a custom converter tied to a custom media type, or
- avoid multiple mappers entirely and use DTOs,
@JsonView, or Jackson annotations.
Option 1: Inject a Secondary Mapper and Serialize Explicitly
This is the most direct and least surprising approach. Keep the default mapper for normal endpoints and inject a second mapper where special serialization is required.
Use it in the controller:
This approach is explicit. When you read the controller, you immediately see that it uses custom JSON rules.
Option 2: Use a Custom Converter for a Specific Media Type
If you want automatic @ResponseBody conversion instead of returning a JSON String, create another MappingJackson2HttpMessageConverter and bind it to a custom media type.
Then the controller opts in by its produced media type:
This is useful when an entire API version has a distinct contract.
Option 3: Do Not Reach for Another Mapper Yet
Multiple mappers add complexity. If the difference is small, a second mapper may be the wrong tool. Consider these alternatives first:
- '
@JsonViewwhen different endpoints expose different field subsets.' - endpoint-specific DTOs when the response contract differs structurally.
- custom serializers for one or two unusual properties.
- '
MappingJacksonValuewhen you want per-request filtering.'
In many codebases, DTOs are easier to test and easier to reason about than many partially overlapping mapper configurations.
What Spring Boot Does by Default
Spring Boot auto-configures one primary mapper and one or more JSON converters that use it. Those converters are shared infrastructure. That is why there is no built-in @UseObjectMapper("name") style switch on a controller.
The framework favors global configuration plus explicit opt-in patterns when an endpoint must behave differently.
Common Pitfalls
Mutating the global ObjectMapper inside a controller is dangerous. The mapper is normally shared, so runtime changes can leak into unrelated requests.
Using multiple mappers for small cosmetic differences creates maintenance debt. If you only need different field names or omitted fields, DTOs or @JsonView are usually cleaner.
Registering a custom converter without a distinct media type can affect other controllers unexpectedly. Make the routing rule explicit.
Returning raw JSON strings everywhere loses some Spring MVC conveniences, including clearer content negotiation and type-based behavior. Use explicit serialization sparingly.
Summary
- Spring Boot does not provide a simple per-controller mapper switch by default.
- The safest controller-specific pattern is injecting a secondary mapper and serializing explicitly.
- For whole endpoint families, a custom media type plus custom converter is often cleaner.
- If the differences are small, DTOs or
@JsonVieware usually better than multiple mappers. - Avoid mutating the shared global mapper during request handling.
Related reading
- Is it possible to rename a maven jar-with-dependencies?
- Is it possible to set a different specification per cache using caffeine in spring boot?
- Is it possible to set private property via reflection?
- Is it possible to use Java 8 for Android development?
- Is it possible to use Java 8 for Android development?
- Is it possible with Spring Boot to serve up JSPs with a JAR packaging?
- Is it safe to get values from a java.util.HashMap from multiple threads no modification?
- Is it safe to get values from a java.util.HashMap from multiple threads no modification?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.