Spring Boot JSON Auto-Configuration — Jackson and ObjectMapper
Fast recall
Quick revision
Core answer
Understanding of Spring Boot auto-configuration for JSON and how Jackson is integrated
Not knowing that spring-boot-starter-web already includes Jackson or trying to manually configure ObjectMapper without understanding auto-configuration
Show how to customize ObjectMapper with application.properties or a custom Bean to override defaults
Interview-ready guide
Interview answer
The web starter commonly brings Jackson, so returning an OrderDto from a RestController with Accept: application/json is written by MappingJackson2HttpMessageConverter. Boot applies Jackson properties and registered modules to its mapper. Returning a String may instead use StringHttpMessageConverter, and an XML converter can produce XML if installed and negotiated. JSON support is conditional infrastructure, not an annotation-level promise that every response is JSON.
Spring Boot JSON Auto-Configuration — Jackson and ObjectMapper
Direct Answer
When Jackson is on the classpath, Spring Boot can auto-configure a Jackson ObjectMapper and Spring MVC can register a Jackson HttpMessageConverter. That makes JSON a common representation, but it does not force every @RestController return value to application/json. Content negotiation, produces, the return type, and all configured converters determine the response.
Code Example
@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper objectMapper() {
var mapper = new ObjectMapper();
// Include non-null fields only (don't serialize null values)
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// Use snake_case for JSON field names instead of camelCase
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
// Write dates as "2024-01-15" instead of timestamps
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return mapper;
}
}
@RestController
class UserController {
@GetMapping("/api/users/{id}")
public User getUser(@PathVariable Long id) {
// This User will be serialized using our custom ObjectMapper
return new User(id, "john_doe", null); // null email won't appear in JSON
}
}
record User(Long id, String username, String email) {}
Common Mistakes & Pitfalls
- Manually adding Jackson dependency: Freshers add jackson-databind to pom.xml when spring-boot-starter-web already includes it. This can cause version conflicts. Always check what your starter includes first.
- Not understanding auto-configuration override: Creating a Bean named objectMapper but not realizing Spring Boot's default is still active because they misspelled the method or used wrong scope. The custom bean must match the name and return type exactly.
- Using @Autowired ObjectMapper in config: Trying to inject ObjectMapper in the same @Configuration class that creates it causes circular dependencies. Define it as a @Bean method, don't inject it.
- Ignoring application.properties: Spring Boot lets you configure common ObjectMapper settings via properties like spring.jackson.default-property-inclusion=non_null. Freshers write a whole @Bean when a single property would work.
Check your understanding
Answer these without looking back at the article.
- How do you configure Jackson to serialize Java 8 LocalDateTime as ISO-8601 string?
- What does fail-on-unknown-properties=false do in Jackson?
- What is the difference between Jackson2ObjectMapperBuilderCustomizer and defining an ObjectMapper bean?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.