Skip to content

How can I force Spring to use a specific (de)serialization framework, when multiple ones are detected in the classpath? #32382

@volkert-fastned

Description

@volkert-fastned

Hello,

I included a library in a Kotlin Spring project that happens to include kotlinx.serialization as a transitive dependency.

However, we are not yet ready to switch the entire Spring application from Jackson to kotlinx.serialization, since that would result in different behavior, and break quite a few things. As a matter of fact, integration tests started to fail because of this exact reason.

Apparently, the preference for kotlinx.serialization over Jackson (if found in the classpath) is hard-coded throughout multiple locations in the Spring source code, through use of a ClassUtils.isPresent("kotlinx.serialization.json.Json", classLoader) check. Examples: AllEncompassingFormHttpMessageConverter, RestTemplate, DefaultRestClientBuilder, etc.

Why is Spring so opinionated on the preferential order when multiple serde frameworks are detected in the classpath and why is this hard-coded in such a way, not to mention in different parts (one RestTemplate, one for Feign, one for MVC, etc)?

Why isn't there a central way to select the preferred serde framework in application.yml, for instance? I believe there used to be such settings in older versions of Spring Boot (spring.http.converters.preferred-json-mapper, spring.mvc.converters.preferred-json-mapper and such), but those didn't do anything for me.

I found several workarounds through @Configuration beans that would have to filter the list of MessageConverters and such, but those don't seem to work for me and are also quite ugly workarounds, even if they would work.

The existence of certain libraries and dependencies (transitive or otherwise) in the classpath should not change application behavior like that, and even if that's somehow inevitable, it should be easy to override this in a convenient and single application-wide way.

Am I missing something?

Any help on forcing the Spring framework to prefer Jackson over kotlinx.serialization application-wide would be helpful.

I'm not ruling out migrating to kotlinx.serialization entirely at some point, but I prefer to first restore the existing behavior before attempting that.

Thanks for any help you can provide with this. 🙏🏽

Metadata

Metadata

Assignees

Labels

for: stackoverflowA question that's better suited to stackoverflow.comin: webIssues in web modules (web, webmvc, webflux, websocket)theme: kotlinAn issue related to Kotlin support

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions