Data class and jackson annotation conflict


#1

I have this data class:

 
public data class BluePrint (
        JsonProperty("id") val id : Int,
        JsonProperty("name") val name : String,
        JsonProperty("content") val content : String) {
}

and i get this error when trying to serialize it using jackson: java.lang.IllegalArgumentException: Conflicting getter definitions for property "id": qs.contracts.BluePrint#getId(0 params) vs qs.contracts.BluePrint#component1(0 params)

when i remove the annotations it works or when i remove the data from the class definition it works. this is because kotlin puts the jackson anotation on the component1 function too.

how can we use the dataclass with jackson annotations?
please help. thanks.


#2

Probably this can help you as a workaround http://wiki.fasterxml.com/JacksonMixInAnnotations If it works, please, tell


#3

Hello!

Thanks for feedback.

The effect you described is a bug. Auto-generated component functions should not be annotated. I reported it.

In general this is a tricky usecase because we are yet to come to a final decision on what annotating a property means. In current version it results in backing field being annotated (but not getter or setter).

The answer would be: use some workaround until the bug is fixed. Here is an ugly one:

data class BluePrint(private val _id: Int) {
    val id: Int [JsonProperty("id")] get() = _id
}

#4

sorry, but this workaround prevents the desirializing done by jackson... for now we are removing the "data" key word... Thanks.


#5

...(see below)


#6

Tomer, you do not need any annotations.  Jackson has a Kotlin module to support data classes directly (and other classes that cannot have a default constructor):

https://github.com/FasterXML/jackson-module-kotlin

version 2.4.3+ support M9 of Kotlin

(note, I am the author, feel free to ask questions here, and report bugs or feature requests in the GitHub issue tracker)