Configuring Jackson serialization in Spring Boot
Jackson is the default JSON serialization and deserialization tool when developing Spring Boot-based applications. It provides powerful features to flexibly process JSON data. However, Jackson's default behavior may not fully meet our needs. For example, date format, null value processing, data accuracy and other issues may require custom configuration. This article will explain in detail how to configure Jackson in Spring Boot to meet these needs.
1. Why do you need to customize Jackson configuration?
Jackson's default behavior is reasonable in most cases, but in actual development we may need to customize the following:
- Date format: By default, Jackson will serialize dates to timestamps, which may not match our needs.
- Null value processing: By default, Jackson ignores null values, but we may need to keep null values.
-
Data Accuracy:for
BigDecimal
andBigInteger
Types such as direct serialization may cause accuracy issues. - Custom serialization: For some complex types, we may need to customize serialization logic.
2. Config
In Spring Boot, you can create a@Configuration
class and define aObjectMapper
Bean customizes Jackson's behavior.
import ; import ; import ; import ; import ; import ; import ; import ; import .; import .; import .; import .; import .; import .; import .; import ; import ; import ; import ; import ; import .Jackson2ObjectMapperBuilder; import ; import ; import ; import ; import ; import ; import ; /** * @author XiaoXin */ @Configuration public class JacksonConfig { @Bean @Primary @ConditionalOnMissingBean() public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ("yyyy-MM-dd HH:mm:ss"); ObjectMapper objectMapper = (false) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .featuresToDisable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE) .timeZone(("Asia/Shanghai")) .build(); // null data return (); (, ); // When deserialization, the mismatch attribute does not throw an exception (DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // When serializing, no exception will be thrown (SerializationFeature.FAIL_ON_EMPTY_BEANS, false); // If it is an invalid subtype during deserialization, no exception will be thrown (DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false); // Don't use the default dateTime for serialization, (SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false); // Data accuracy issues SimpleModule simpleModule = new SimpleModule() .addSerializer(, ) .addSerializer(, ) .addSerializer(, ) .addSerializer(, ); (simpleModule); // Configure Java 8 time and date module JavaTimeModule javaTimeModule = new JavaTimeModule(); (, new LocalDateTimeSerializer(("yyyy-MM-dd HH:mm:ss"))); (, new LocalDateSerializer(("yyyy-MM-dd"))); (, new LocalTimeSerializer(("HH:mm:ss"))); (, new LocalDateTimeDeserializer(("yyyy-MM-dd HH:mm:ss"))); (, new LocalDateDeserializer(("yyyy-MM-dd"))); (, new LocalTimeDeserializer(("HH:mm:ss"))); (javaTimeModule).registerModule(new ParameterNamesModule()); return objectMapper; } }
This is the end of this article about configuring Jackson serialization in Spring Boot. For more related Spring Boot Jackson serialization content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!