https://kotlinlang.org logo
#spring
Title
# spring
g

George

07/29/2023, 12:13 PM
Hi folks, i am using springboot with koltin with functional apis. I trying to handle common exceptions in incoming request. More specifically, i try to separate serialization exception and publisher exceptions in ServerRequest.awaitBody() like this:
Copy code
suspend inline fun <reified T : Any> ServerRequest.awaitAndReceive(): T {
    val body = awaitBodyOrNull<String>()
    requireNotNull(body) { bodyTypeErrorMessage<T>() }
    return deserializeBody(body)
}

inline fun <reified T : Any> deserializeBody(body: String): T {
    try {
        return Json.decodeFromString<T>(body)
    } catch (e: IllegalArgumentException) {
        throw IllegalArgumentException(bodyTypeErrorMessage<T>())
    }
}
Question is: do i deserialize the request two times in this case? 1 for awaitBodyOrNull<String>() and 1 manually?
Answering my own question for anyone interested: After playing around with the debugger i figured out that indeed this triggers 2 deserializations (tbh that's the expected). But it turns out for now the
KotlinSerializationStringDecoder
, also deserializes it in two steps (like my code pretty much), though this is a current limitation from
kotlinx.serialization
and will probably lifted in future.
8 Views