Context
We stored our data as JSON as below
{
data: {
name: "hello",
project: {
value: "demo project"
}
}
}
@Document(collection = "tasks")
class Task {
var data: HashMap<String, TaskFieldValue> = HashMap()
}
class TaskFieldValue() {
var value: String = ""
}
Problems
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [io.nettable.entity.TaskFieldValue]
Solution
@Configuration
class MongoConfiguration {
@Bean
fun customConversions(): MongoCustomConversions {
val converters = ArrayList<Any>()
converters.add(String2NetValue())
return MongoCustomConversions(converters)
}
@ReadingConverter
class String2NetValue : Converter<String, NetTaskFieldValue> {
override fun convert(source: String): NetTaskFieldValue {
val value = NetTaskFieldValue()
value.value = source
return value
}
}
}