Kotlinx serialization with Spring Boot

Has anyone tried this? The code that works great with KTOR rest seems to not work correctly on the spring boot side. I am guessing spring-boot is just using Jackson by default. What verbiage can i search for to figure out how to override that and use Kotlin serialization instead of Jackson.

We have used kotlinx.serialization with some (experimental) spring-boot apps.

I have uploaded the code to https://github.com/markt-de/spring-kotlinx-serialization, but there is no README yet.
It only supports WebApplicationType.REACTIVE apps so far, but those can be easily set up by applying the webflux-starter dependency:

implementation("de.classyfi.boot:spring-kotlinx-serialization-starter-webflux:0.0.1-20200318")

I have published a snapshot to https://bintray.com/markt-de/snapshots/spring-kotlinx-serialization in case you want to try it.

how would i set up the repo on this?
I am dying to try it, want to use sealed classes as rest repsonses

This should do the trick:

repositories {
  jcenter()
  maven { url = uri("https://dl.bintray.com/markt-de/snapshots/") }
}

dependencies {
  implementation("de.classyfi.boot:spring-kotlinx-serialization-starter-webflux:0.0.1-20200318")
}

I’m looking forward to your feedback!

i cant wait, after using kotlinx serialization on ktor, i am missing in on springboot

I was able to remove the dependency on com.fasterxml.jackson.module:jackson-module-kotlin:2.10.3 and the project still worked.

trying it out in a kotlin multi-platform project
the following code still just pushing out the success portion of the sealed class

@PostMapping(STUB_SEALED_TEST)
fun testSealed():TestSealedClass<List<PrintShopDTO>>{
    val all = repo.findAll().map { it.toDto() }
    return TestSealedClass.Success(all)
}

her is the sealed class

@Serializable
sealed class TestSealedClass <out T>{
    @Serializable
    data class Failure<out T>(val msg: String) : TestSealedClass<T>()
    @Serializable
    data class Success<out T>(val payload: T) :  TestSealedClass<T>()
}

and here is the js side ingestion

suspend fun testSealed() {
    val url ="$REST_SERVICE_ADDRESS/${CTX_PRINT_SHOP.getStub()}/$STUB_SEALED_TEST"
    val response = makeXMLHttpRequest(url,"", HttpMethod.POST, CTX_PRINT_SHOP, "testSealed")
    console.log(response)
    val parsed = Json.parse(deserializer = TestSealedClass.serializer(typeSerial0 = PrintShopDTO.serializer().list), string = response)
    console.log(parsed)

}

the browser side error on parse

JsonDecodingException: Invalid JSON at 0: Expected '[, kind: SEALED

the string data looks like this:

{“payload”:[{“id”:1,“dateCreated”:{“year”:2020,“month”:2,“day”:24,“hour”:10,“minute”:29,“second”:0,“milliSecond”:0,“jdTime”:217249350},“dateUpdated”:{“year”:2020,“month”:2,“day”:24,“hour”:10,“minute”:29,“second”:0,“milliSecond”:0,“jdTime”:217249350},“title”:“Everyday havent fam air chic hexagon fam truck mi unicorn bottle selfies cray moon humblebrag.”,“books”:[{“id”:2,“dateCreated”:{“year”:2020,“month”:2,“day”:24,“hour”:10,“minute”:29,“second”:0,“milliSecond”:0,“jdTime”:217249418},“dateUpdated”:{“year”:2020,“month”:2,“day”:24,“hour”:10,“minute”:29,“second”:0,“milliSecond”:0,“jdTime”:217249418},"d …

using the 0.14.0 version of kotlinx serialization

going to test some non sealed class stuff in a bit

oddly i am still getting jackson errors - i must be doing something wrong… Project builds. Build.gradle

jvmMain {
    dependencies {
        implementation kotlin('stdlib-jdk8')
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:$v_serialization")
        implementation 'de.classyfi.libs:spring-kotlinx-serialization:0.0.1-20200318'
        //implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.10.3")
        implementation("org.springframework.boot:spring-boot-starter-web")
        implementation("org.springframework.boot:spring-boot-starter-data-jpa")
        implementation("org.springframework.boot:spring-boot-starter-websocket")
        implementation("org.jetbrains.kotlin:kotlin-reflect")
        runtimeOnly("com.h2database:h2")
    }
}

my classes

@Serializable
abstract class TDetail

@Serializable
data class AdminDetail(val alergic:Boolean) : TDetail()

@Serializable
data class CustomerDetail(val dogName:String) : TDetail()

@Serializable
data class TSecUserREQ(
    val fname:String,
    val detail: TDetail
)

val simplePolymorphicModule = SerializersModule {
    polymorphic<TDetail> {
        AdminDetail::class with AdminDetail.serializer()
        CustomerDetail::class with CustomerDetail.serializer()
    }
}

sending it too spring via

suspend fun testDeep(){
    val json = Json(context = simplePolymorphicModule)
    val user = TSecUserREQ(fname = "Chris", detail = CustomerDetail(dogName = "buddy"))
    val userSerial = json.stringify(TSecUserREQ.serializer(), user)
    val url ="$REST_SERVICE_ADDRESS/${CTX_SEC_USER.getStub()}"
    val response = makeXMLHttpRequest(url,userSerial, HttpMethod.POST, CTX_PRINT_SHOP, "testDeep")
}

the error :com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of net.ideablender.foundation.req.TSecUserREQ (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

As I said, the library so far only supports reactive webapps (spring-boot-starter-webflux). Servlet apps (spring-boot-starter-web) are currently not supported.

Pull requests are of course more than welcome.

ty for the project, i need to read up more on webflux