4

I have several Kotlin data classes that implement this TypeAlias interface:

import com.fasterxml.jackson.annotation.JsonValue;

public interface TypeAlias<T> {
    @JsonValue
    T getValue();
}

Previously, I'd been using an earlier version of Jackson and they deserialized fine. But now I'm using 2.9.9.

One class that fails to deserialize:

data class UserId(@NotNull private val value: EntityId) : TypeAlias<EntityId> {
    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    constructor(value: String) : this(EntityId(value))

    override fun getValue() = value
}

Test for it:

@Test
    fun `Deserialization test`() {
        val userIdString = "\"aaaaaaaaaaaaaaaaaaaaaa\""
        mapper.readValue(userIdString, UserId::class.java)
    } 

This test blows up with:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `UserId` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('aaaaaaaaaaaaaaaaaaaaaa')

My mapper does have the KotlinModule registered.

Note that UserId does have a JsonCreator on its secondary String constructor. The behavior makes me think that the interface is causing Jackson to not see the @JsonCreator annotation. What am I missing here?

0

3 Answers 3

2

You need a valid Jackson config. Take a look at this example

@Configuration
class JacksonConfig {    
    @Bean
    fun objectMapper(): ObjectMapper {
        val objectMapper = ObjectMapper()    
        val jdk8Module = Jdk8Module().configureAbsentsAsNulls(true)
        objectMapper.registerModule(jdk8Module)
        objectMapper.registerModule(JavaTimeModule())
        // ... your customizations
        objectMapper.registerKotlinModule()
        return objectMapper
    }
}
1

After digging in with a couple coworkers, we realized that the problem had to do with the order in which modules were being registered on the ObjectMapper. We were registering a module after the Kotlin module that tampered with the config. When we instead registered the Kotlin module after the offending module, everything worked again.

2
  • 1
    It works like a charm (no need to add annotations for all classes). I had exactly same issues. thanks!
    – makerj
    Jan 25, 2021 at 14:48
  • Glad to hear it was helpful! Jan 25, 2021 at 21:57
0

I used 2.9.9 with the below code and i was able to deserialize. (Sorry about putting this in the answer block, couldnt format it right in the comments section)

data class abc( private val value: def?)  {

    @JsonCreator(mode =JsonCreator.Mode.DELEGATING)
    constructor( s: String) : this(def(s)) {
        println(s)
    }

}

data class def(val data: String)

fun main(args: Array<String>) {
    println("Hello World!")
    val m: ObjectMapper =  ObjectMapper()
    m.readValue("\"5abc\"", abc::class.java)
}

could you post a git link to a simple repo where we could reproduce the issue?

5
  • Thanks Arpan. I see that there's not an interface being used here, though? I'm able to deserialize if I don't use an interface. As far as the repo goes, it's an internal company repo so I'm not able to share it. Jul 16, 2019 at 19:58
  • I used an interface and i still dont get the error.. Below is the code.. Sorry about the formatting Jul 16, 2019 at 23:23
  • fun main(args: Array<String>) { val m: ObjectMapper = ObjectMapper() // m.readValue("\"5abc\"", abc::class.java) val user = m.readValue("\"5abc\"", UserId::class.java) println(user.getValue()) } interface TypeAlias<T> { @JsonValue fun getValue(): T } data class UserId(private val value: EntityId) : TypeAlias<EntityId> { @JsonCreator(mode = JsonCreator.Mode.DELEGATING) constructor(value: String) : this(EntityId(value)) { println(value) } override fun getValue() = value } data class EntityId(val value: String) Jul 16, 2019 at 23:24
  • Thanks. You're right, it works with an interface. After digging in with a couple coworkers, we realized that the problem had to do with registering a module after the Kotlin module that tampered with the config. When we bound the Kotlin module afterwards, everything worked again. Jul 16, 2019 at 23:41
  • Thanks again for your help! Jul 16, 2019 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.