Skip to content

Jackson2ObjectMapperBuilderCustomizer does not support Jackson JsonReadFeature, JsonWriteFeature, StreamWriteFeature #31230

@chorar

Description

@chorar

Description

When customizing Jackson's ObjectMapper via Jackson2ObjectMapperBuilderCustomizer in Spring Boot, attempting to configure JsonReadFeatures like below causes IllegalArgumentException:

import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; @Slf4j @AutoConfiguration(before = JacksonAutoConfiguration.class) public class JacksonConfig { @Bean private Jackson2ObjectMapperBuilderCustomizer customize() { return builder -> { builder.featuresToEnable(JsonReadFeature.ALLOW_SINGLE_QUOTES); builder.featuresToEnable(JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS); builder.featuresToEnable(StreamWriteFeature.WRITE_BIGDECIMAL_AS_PLAIN);	};	} }

This is because Jackson2ObjectMapperBuilder currently only supports configuring a limited set of feature types including JsonParser.Feature, JsonGenerator.Feature, SerializationFeature, DeserializationFeature and MapperFeature.
But JsonReadFeature, JsonWriteFeature, StreamWriteFeature introduced in Jackson 2.10 is not supported yet. So enabling it via customize() method throws exception:
IllegalArgumentException: Unknown feature class: com.fasterxml.jackson.core.json.JsonReadFeature
IllegalArgumentException: Unknown feature class: com.fasterxml.jackson.core.json.JsonWriteFeature
IllegalArgumentException: Unknown feature class: com.fasterxml.jackson.core.json.StreamWriteFeature

Root Cause

private void configureFeature(ObjectMapper objectMapper, Object feature, boolean enabled) { if (feature instanceof JsonParser.Feature jsonParserFeature) { objectMapper.configure(jsonParserFeature, enabled);	} else if (feature instanceof JsonGenerator.Feature jsonGeneratorFeature) { objectMapper.configure(jsonGeneratorFeature, enabled);	} else if (feature instanceof SerializationFeature serializationFeature) { objectMapper.configure(serializationFeature, enabled);	} else if (feature instanceof DeserializationFeature deserializationFeature) { objectMapper.configure(deserializationFeature, enabled);	} else if (feature instanceof MapperFeature mapperFeature) { objectMapper.configure(mapperFeature, enabled);	} else { throw new IllegalArgumentException("Unknown feature class: " + feature.getClass().getName());	} }

The root cause is org.springframework.http.converter.json.Jackson2ObjectMapperBuilder#configureFeature() method hardcoded checking for those feature types but not handling JsonReadFeature, JsonWriteFeature, JsonWriteFeature.

Versions

This issue occurs on latest Spring Boot 3.1.3 with Jackson 2.15.2.

Suggestion

It is suggested that Spring Framework should add support for JsonReadFeature, JsonWriteFeature, StreamWriteFeature in a future release by mapping it to corresponding MapperFeature, or identifying their parent classes like JacksonFeature and ConfigFeature, to maintain compatibility with latest Jackson versions when customizing ObjectMapper.

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions