InvalidDefinitionException when deserializing Kotlin class with interface

davidmerrick :

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?

davidmerrick :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=154987&siteId=1