Created
June 15, 2020 08:52
-
-
Save e5l/3b4d5d704b4d7c6e2a65cf68de8e9ca4 to your computer and use it in GitHub Desktop.
XML serialization with ktor client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | |
plugins { | |
kotlin("jvm") version "1.3.72" | |
kotlin("plugin.serialization") version "1.3.72" | |
application | |
} | |
group = "me.leonid" | |
version = "1.0-SNAPSHOT" | |
val ktor_version = "1.3.2" | |
repositories { | |
mavenCentral() | |
jcenter() | |
} | |
dependencies { | |
testImplementation(kotlin("test-junit5")) | |
implementation(kotlin("stdlib-jdk8")) | |
implementation("io.ktor:ktor-server-cio:$ktor_version") | |
implementation("net.devrieze:xmlutil-serialization-jvm:0.20.0.1") | |
implementation("io.ktor:ktor-client-cio:$ktor_version") | |
implementation("io.ktor:ktor-client-json-jvm:$ktor_version") | |
} | |
tasks.withType<KotlinCompile>() { | |
kotlinOptions.jvmTarget = "1.8" | |
} | |
application { | |
mainClassName = "MainKt" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.ktor.application.* | |
import io.ktor.client.* | |
import io.ktor.client.call.* | |
import io.ktor.client.features.json.* | |
import io.ktor.client.request.* | |
import io.ktor.client.statement.* | |
import io.ktor.http.* | |
import io.ktor.http.content.* | |
import io.ktor.request.* | |
import io.ktor.response.* | |
import io.ktor.routing.* | |
import io.ktor.server.cio.* | |
import io.ktor.server.engine.* | |
import io.ktor.utils.io.core.* | |
import kotlinx.coroutines.* | |
import kotlinx.serialization.* | |
import nl.adaptivity.xmlutil.serialization.* | |
@OptIn(ImplicitReflectionSerializer::class) | |
class XMLSerializer(private val format: XML = XML()) : JsonSerializer { | |
override fun read(type: TypeInfo, body: Input): Any { | |
val text = body.readText() | |
val deserializationStrategy = format.context.getContextual(type.type) | |
val mapper = deserializationStrategy | |
?: type.kotlinType?.let { serializer(it) } | |
?: type.type.serializer() | |
return format.parse(mapper, text) ?: error("Failed to parse response of type $type. The result is null.") | |
} | |
override fun write(data: Any, contentType: ContentType): OutgoingContent { | |
val serializer = data::class.serializer() as KSerializer<Any> | |
val text = format.stringify(serializer, data, null) | |
return TextContent(text, contentType) | |
} | |
} | |
@Serializable | |
data class User(val id: Int, val name: String) | |
fun main(args: Array<String>) = runBlocking { | |
val server = embeddedServer(CIO, port = 8080) { | |
routing { | |
post("/") { | |
println(call.request.headers[HttpHeaders.ContentType]) | |
println(call.receive<String>()) | |
call.respond(HttpStatusCode.OK) | |
} | |
} | |
}.start(wait = false) | |
delay(2000) | |
val client = HttpClient { | |
Json { | |
serializer = XMLSerializer() | |
accept(ContentType.Application.Xml) | |
} | |
} | |
val user = User(0, "Leonid") | |
val response: HttpResponse = client.post("http://127.0.0.1:8080") { | |
contentType(ContentType.Application.Xml) | |
body = user | |
} | |
println(response.status) | |
server.stop(1000L, 1000L) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment