The issue, really, is in Jackson's API here. Here's how the readValue method is declared:

public <T> T readValue(String content, TypeReference valueTypeRef)

They are using the raw type of TypeReference for some reason, even though they could easily take a TypeReference<T> as their parameter. If they did, you code would work as is, as Kotlin could then infer the T generic type parameter of the function, and therefore know its return type.

You can work around this issue a couple different ways, however, by being explicit with your types.

  • Either by providing the generic type parameter for the function, and having the type of the msg variable inferred:

      val typeRef: TypeReference<Map<K, V>> = object : TypeReference<Map<K, V>>() {} 
      val msg = objectMapper.readValue<HashMap<String, String>>(message.payload, typeRef)
    
  • Or alternatively, by explicitly typing your variable, and having the function's type parameter inferred:

      val msg: HashMap<String, String> = objectMapper.readValue(message.payload, typeRef)
    
Answer from zsmb13 on Stack Overflow
🌐
Baeldung
baeldung.com › home › libraries › jackson support for kotlin
Jackson Support for Kotlin | Baeldung on Kotlin
August 26, 2026 - import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.KotlinModule val mapper = ObjectMapper().registerModule(KotlinModule())
🌐
Mario Fernandez
hceris.com › painless json with kotlin and jackson
Painless JSON with Kotlin and jackson | Mario Fernandez
June 16, 2019 - @Configuration class JacksonConfiguration { @Bean fun mappingJackson2HttpMessageConverter(): MappingJackson2HttpMessageConverter { return MappingJackson2HttpMessageConverter().apply { this.objectMapper = ObjectMapper().apply { registerModule(KotlinModule()) } } } }
🌐
GitHub
github.com › ProjectMapK › KMapper
GitHub - ProjectMapK/KMapper: Object to Object mapper Libraly for Kotlin. · GitHub
January 20, 2023 - KMapper is a object to object mapper library for Kotlin, which provides the following features.
🌐
Stack Abuse
stackabuse.com › reading-and-writing-json-in-kotlin-with-jackson
Reading and Writing JSON in Kotlin with Jackson
February 27, 2023 - import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper // Registering the Kotlin module with the ObjectMapper instance val mapper = jacksonObjectMapper() // JSON String val jsonString = """{ "id":101, "username":"admin", "password":"Admin123", "fullName":"Best Admin" }""" With that done, we can pass JSON contents into our ObjectMapper methods, such as readValue(), as usual for Jackson: // Read data from a JSON string val userFromJson = mapper.readValue<User>(jsonString) // Or val userFromJsonWithType: User = mapper.readValue(jsonString) If we print the value of userFromJson we'll see something like this: User(id=101, username=admin, password=Admin123, fullName=Best Admin) The readValue() function can be used with or without the Class parameter, as you saw a little earlier with the two variables (userFromJson and userFromJsonWithType).
🌐
DEV Community
dev.to › krud › object-mapping-pros-cons-30ln
Kotlin Object Mapping - Pros & Cons - DEV Community
August 10, 2022 - In some cases we can't change the data classes code (or don't want to). For these use cases we create a standalone mapper between the two classes using Kotlin DSL.
🌐
Medium
medium.com › @shrikantjagtap99 › kotlin-map-serialization-and-deserialization-with-jackson-6343d9e56107
Kotlin Map Serialization and Deserialization with Jackson | by Shrikant Jagtap | Medium
April 26, 2020 - In order to serialize/deserialize the data classes, we need an instance of ObjectMapper class for Kotlin and register KotlinModule.
🌐
Medium
bijukunjummen.medium.com › jackson-kotlin-extension-and-reified-types-2143015df62a
Jackson Kotlin extension and reified types | by Biju Kunjummen | Medium
May 30, 2023 - import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue val objectMapper = jacksonObjectMapper() val result: Map<String, List<String>> = objectMapper.readValue(json)
Find elsewhere
🌐
Educative
educative.io › answers › parsing-api-data-in-kotlin-using-jackson
Parsing API data in Kotlin using Jackson
implementation(dependencyNotation: "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.7") Next, we need an instance of the JacksonObjectMapper in our Repository layer. This enables us to use the functions Jackson provides for parsing data: val objectMapper = jacksonObjectMapper() Now, we can use the objectMapper to read the value of our response body and parse it into a JsonNode.
🌐
BezKoder
bezkoder.com › home › kotlin – convert json to object and vice versa using jackson
Kotlin – Convert JSON to Object and vice versa using Jackson - BezKoder
December 13, 2020 - Anywhere you want to make Jackson work, just follow two steps: – import com.fasterxml.jackson.module.kotlin.* – use ObjectMapper instance like this: val mapper = jacksonObjectMapper() // Json String/URL/File to Object var person: Person = -> mapper.readValue ·
🌐
Kotlinlang
slack-chats.kotlinlang.org › t › 16665966 › i-have-this-simple-data-class-what-should-i-do-to-make-it-po
I have this simple data class What should I do to make it po kotlinlang #getting-started
March 14, 2024 - val mapper = jacksonObjectMapper(), that's using the Kotlin module and you're good 👍. But if you just have · val mapper = ObjectMapper(), with no extra Kotlin module being registered, that could definitely explain the issues you're seeing.
🌐
Medium
medium.com › @bartlomiej.bobak › deserializing-a-kotlin-object-using-jackson-may-not-work-as-you-expect-1e01003cc0f0
Deserializing a Kotlin object using Jackson may not work as you expect | by Bartłomiej Bobak | Medium
February 12, 2023 - @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, property = "type" ) sealed class Scope { object All : Scope() data class Specific(val value: String): Scope() } fun main() { val objectMapper = ObjectMapper().registerKotlinModule() val json = objectMapper.writeValueAsString(Scope.All) val scope = objectMapper.readValue<Scope>(json) when (scope) { Scope.All -> println("All") is Scope.Specific -> println(scope.value) } }
🌐
Medium
proandroiddev.com › parsing-optional-values-with-jackson-and-kotlin-36f6f63868ef
Parsing data with Jackson using kotlin | ProAndroidDev
May 7, 2020 - The KotlinModule can be registered at runtime with mapper object as : val mapper = ObjectMapper().registerModule(KotlinModule()) Now there is no need to add @JsonProperty to data/model classes. KotlinModule internally uses Kotlin specific reflection APIs which handles the mapping of KClass, KFunction to java classes with the help of kotlin.jvm and kotlin.reflect.jvm packages.
🌐
GitHub
github.com › flock-community › kmapper
GitHub - flock-community/kmapper: Kotlin object mapper · GitHub
A Kotlin compiler plugin that automatically generates mapping methods between data classes using a fluent DSL.
Author: flock-community
🌐
Continuous Growth
kapentaz.github.io › home › kotlin › json
Kotlin and Jackson (ObjectMapper) - Continuous Growth
March 14, 2020 - 다행히도 jackson-module-kotlin 모듈에서 KotlinModule 클래스를 통해서 문제를 해결할 수 있습니다. ObjectMapper 객체를 생성해서 모듈을 직접 지정할 수도 있고 Jackson에서 제공하는 확장 함수로 쉽게 사용할 수도 있습니다.
🌐
Developwith
developwith.com › home › "developwith — developer tips" › "how-to guides" › "kotlin" › "how do i configure jackson object mapper?"
How do I configure jackson object mapper? - developwith
April 10, 2019 - When there is a problem trying to deserialize or serialize an object from Kotlin into json, you may need to create your own way to customize the json serialization process. In order to do this in Spring and Kotlin, you’ll need to create a configuration for Jackson that will override the ObjectMapper builder.
🌐
Baeldung
baeldung.com › home › kotlin › kotlin collections › convert a kotlin data class object to a map
Convert a Kotlin Data Class Object to a Map | Baeldung on Kotlin
March 19, 2024 - <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-kotlin</artifactId> <version>2.11.3</version> </dependency> First of all, let’s create an ObjectMapper instance with KotlinModule registered: val DEFAULT_JACKSON_MAPPER = ObjectMapper().registerModule(KotlinModule()) Jackson will convert all values to primitive types.
🌐
Medium
medium.com › simars › kotlin-extension-functions-make-any-class-have-what-you-wish-for-37085e71a4c0
Kotlin | Extension Functions, make any class have what you wish for | by Simar Paul Singh | Simar's blog | Medium
October 3, 2018 - You can find more on these special ... on Kotlin’s official documentation here. We will take a simple and useful example, very common class, JsonNode from fasterxml.jackson. . Transforming JsonNode (tree style) object, is not convenient as modifying a Map<String, Any>. However, ObjectMapper comes with ...
🌐
GitHub
github.com › FasterXML › jackson-module-kotlin › issues › 606
ObjectMapper.registerKotlinModule does not works · Issue #606 · FasterXML/jackson-module-kotlin
November 18, 2022 - data class Foo(val bar: Int) class SerializerTestcase { @Test fun testcase1() { // Initialize serializer val serializer = Jackson2JsonRedisSerializer(Any::class.java) val objectMapper = ObjectMapper().registerKotlinModule() objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY) objectMapper.activateDefaultTyping(objectMapper.polymorphicTypeValidator, ObjectMapper.DefaultTyping.NON_FINAL) serializer.setObjectMapper(objectMapper) // Exception demo val input = Foo(0) println(input) // It prints: Foo(bar=0) val bytes = serializer.serialize(input) println(bytes.toString(Sta
Author: FasterXML