1

I am wondering how I can configure kotlinx as a default serialization in Spring Boot app.

For Jackson, I would use e.g. spring.jackson.deserialization.fail-on-unknown-properties=false

But I haven't found any configuration options for kotlinx in Spring.

I know kotlinx supports this, I only cannot find a way to configure it on Spring level so that it works for example in controller method signatures:

@PostMapping("/foo")
fun fooMethod(
    @RequestBody fooJsonRequest: SomeDataClassRepresentingTheJson,
) {}

↑ Throws HttpMessageNotReadableException exception suggesting I use ignoreUnknownKeys=true when constructing kotlinx.serialization.json.Json. However I do not know where to do this so it would apply to Spring itself.

I have tried creating a bean providing Json object to no avail.

PunchyRascal
  • 280
  • 2
  • 9

1 Answers1

1

Publish a @Bean of the following type and customize Json constructor as needed:

@Bean
fun messageConverter(): KotlinSerializationJsonHttpMessageConverter {
    return KotlinSerializationJsonHttpMessageConverter(Json {
        ignoreUnknownKeys = true
    })
}
PunchyRascal
  • 280
  • 2
  • 9