So I've seen that kotlinx serialization can be use...
# spring
d
So I've seen that kotlinx serialization can be used just by adding it to the classpath, and that it only handles classes marked with
@Serializable
and primitive collections before forwarding everything to Jackson. I've run into an issue where I want to serialize a
List
(I believe the implementation is just a regular
ArrayList
) of a serializable class that has some
@SerialName
annotations on the properties, but it looks like it's being forwarded to Jackson and the
@SerialName
names are being ignored. Is this functionality supported, and how would I update the configuration of my WebFlux project to use kotlinx serialization for this?
y
you can create configuration bean in which you can check if kotlinx.serialization converter is exist and in what order, if it before jackson or after it. you also can override converters with your own configuration. it should be something like this:
Copy code
@Configuration
class KotlinxSerializationConfig : WebFluxConfigurer {
    override fun extendMessageConverters(converters: MutableList<HttpMessageConverter<*>>) {
       // here you can check converters list
    }
}
s
Remove Jackson dependency, if you don't need it specifically. Having multiple serialisation libs will give you pain sooner or later
d
Looking more at the problem, I think the issue with the list is that the kotlinx serialization encoder/decoder implementation in Spring doesn't support polymorphism yet.
As for removing Jackson altogether, that isn't always possible if you're using actuator or something like that, unfortunately
r
The Spring documentation for kotlinx.serialization is sparse. According to this document from Jetbrains, you need to explicitly exclude
spring-boot-starter-json
. Gradle
Copy code
implementation("org.springframework.boot:spring-boot-starter-web") {
   exclude(module = "spring-boot-starter-json")
}
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0")
Maven
Copy code
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-json</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
Tangential to this issue, can someone elucidate why this works for
@RestController
annotated methods but not the router DSL? The latter throws an exception (posted in thread).
Can't attach multiple files to a message. This is part of the exception.
s
I'll assume you are using Kotlin serialisation library. So you need to mark Post class with @Serializable.
r
@Sourabh Rawat: Apologies, I omitted the class declaration in my ad-hoc code snippet. It is annotated with
@Serializable
in the actual code. The strange part is that it's getting serialized by
kotlinx.serialization
when using
@RestControler
, but not when using the router DSL.