From d673a7ae1004a3ac8185e0ca35b6e25dfd7d9d84 Mon Sep 17 00:00:00 2001 From: joffrey-bion Date: Fri, 27 Nov 2020 14:35:41 +0100 Subject: Use Kotlinx Serialization in Spring Boot instead of Jackson --- sw-server/build.gradle.kts | 5 ++--- .../org/luxons/sevenwonders/server/config/WebSocketConfig.kt | 7 +++++++ .../sevenwonders/server/controllers/GameBrowserController.kt | 6 ++++-- .../org/luxons/sevenwonders/server/controllers/LobbyController.kt | 4 +++- 4 files changed, 16 insertions(+), 6 deletions(-) (limited to 'sw-server') diff --git a/sw-server/build.gradle.kts b/sw-server/build.gradle.kts index 2e281a12..96990dc7 100644 --- a/sw-server/build.gradle.kts +++ b/sw-server/build.gradle.kts @@ -1,6 +1,7 @@ plugins { kotlin("jvm") kotlin("plugin.spring") + kotlin("plugin.serialization") id("org.springframework.boot") version "2.4.0" } @@ -12,21 +13,19 @@ dependencies { implementation(project(":sw-bot")) implementation(kotlin("reflect")) // required by Spring 5 implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1") implementation("org.springframework.boot:spring-boot-starter-websocket") implementation("org.springframework.boot:spring-boot-starter-security") // required by spring security when using websockets implementation("org.springframework.security:spring-security-messaging") - implementation("com.fasterxml.jackson.module:jackson-module-kotlin") - implementation("ch.qos.logback:logback-classic:1.1.8") testImplementation(kotlin("test")) testImplementation(kotlin("test-junit")) testImplementation(project(":sw-client")) testImplementation("org.springframework.boot:spring-boot-starter-test") - testImplementation("com.fasterxml.jackson.module:jackson-module-kotlin") } tasks.processResources { diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/WebSocketConfig.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/WebSocketConfig.kt index aad1c2ce..2782c670 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/WebSocketConfig.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/config/WebSocketConfig.kt @@ -3,6 +3,8 @@ package org.luxons.sevenwonders.server.config import org.luxons.sevenwonders.model.api.SEVEN_WONDERS_WS_ENDPOINT import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration +import org.springframework.messaging.converter.KotlinSerializationJsonMessageConverter +import org.springframework.messaging.converter.MessageConverter import org.springframework.messaging.simp.config.ChannelRegistration import org.springframework.messaging.simp.config.MessageBrokerRegistry import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler @@ -48,4 +50,9 @@ class WebSocketConfig( override fun configureClientInboundChannel(registration: ChannelRegistration) { registration.interceptors(topicSubscriptionInterceptor) } + + override fun configureMessageConverters(messageConverters: MutableList): Boolean { + messageConverters.add(KotlinSerializationJsonMessageConverter()) + return true + } } diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt index b63f6756..587afc10 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/GameBrowserController.kt @@ -36,7 +36,7 @@ class GameBrowserController( * @return the current list of [Lobby]s */ @SubscribeMapping("/games") // prefix /topic not shown - fun listGames(principal: Principal): Collection { + fun listGames(principal: Principal): List { logger.info("Player '{}' subscribed to /topic/games", principal.name) return lobbyRepository.list().map { it.toDTO() } } @@ -61,7 +61,9 @@ class GameBrowserController( // notify everyone that a new game exists val lobbyDto = lobby.toDTO() - template.convertAndSend("/topic/games", listOf(lobbyDto)) + // we need to pass a non-generic type (array is fine) so that Spring doesn't break when trying to find a + // serializer from Kotlinx Serialization + template.convertAndSend("/topic/games", listOf(lobbyDto).toTypedArray()) return lobbyDto } diff --git a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt index fccb9b5b..7757c9d9 100644 --- a/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt +++ b/sw-server/src/main/kotlin/org/luxons/sevenwonders/server/controllers/LobbyController.kt @@ -113,7 +113,9 @@ class LobbyController( lobby.getPlayers().forEach { template.convertAndSendToUser(it.username, "/queue/lobby/updated", lobby.toDTO()) } - template.convertAndSend("/topic/games", listOf(lobby.toDTO())) + // we need to pass a non-generic type (array is fine) so that Spring doesn't break when trying to find a + // serializer from Kotlinx Serialization + template.convertAndSend("/topic/games", listOf(lobby.toDTO()).toTypedArray()) } /** -- cgit