How can I configure Spring Webflux to use Kotlinx ...
# spring
e
How can I configure Spring Webflux to use Kotlinx Serialization over Jackson, even though both are on classpath? I can't exclude Jackson completely due to using libs which depend on it.
s
When both are on the classpath, if your class to serialize are properly annotated Kotlinx Serialization should be properly used.
Are you seeing a different behavior?
e
I think so. My class (from gradle module
api
):
Copy code
@Serializable // from kotlinx.serialization
data class RaceDay(
   val eventCode: EventCode,
   val date: LocalDateAsString, // type-alias configuring LocalDate to use a specific serializer
)
Controller method from different gradle module `app`:
Copy code
@GetMapping("/link")
   @LogStackCoroutine
   suspend fun getActiveLinks(): ResponseEntity<List<RaceDay>> =
      ResponseEntity.ok(orchestrator.activeLinks())
response:
Copy code
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 49

[
    {
        "date": "1970-03-11",
        "eventCode-U8HkqSU": "PEW"
    }
]
app has dependency on
org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0
If I change the controller method to use Kotlinx Serialization directly it gets rid of the
-U8HkqSU
which I believe comes from jackson trying to serialize a value class. ``````
s
Could be due to combining suspending function with wrapper like
ResponseEntity
or to https://github.com/Kotlin/kotlinx.serialization/issues/2060. Could you please try with/without suspending and with/without
ResponseEntity
and let me know the 4 results?
e
ok
Suspend with ResponseEntity Suspend without ResponseEntity No suspend with ResponseEntity No suspend without ResponseEntity
s
Hum, "No suspend without ResponseEntity " is weird.
e
Yes 🙂 I ran it again now though just to double-check, still
s
Ok, please share a repro and I will have a look later once I have a bit of time.
👍 1
e
s
Looks like there is a bug in
AbstractMessageWriterResultHandler#writeBody
for those use cases with a confusion between leveraging
bodyParameter
versus
actualParam
. Could you please create an issue in https://github.com/spring-projects/spring-framework/issues pinging me (
@sdeleuze
) and linking your repro?
e
Absolutely. Thanks for taking a look into it 🌟
In case anyone else comes looking into this convo.. issue is here: https://github.com/spring-projects/spring-framework/issues/30214
s
Fix merged, will be fixed in Spring Boot
2.7.11
and
3.0.6
. If you can test to confirm it works (when CI builds will be done) with snapshots that would be great.
🦜 2
e
Will do :)
Nice work 🙌
🎉 1
294 Views