首页 > 解决方案 > Cannot deserialize instance of `com.example.million.model.Domain` out of START_ARRAY token

问题描述

I'm using Kotlin, Spring Boot, Jackson dataformat csv. Not sure, how I can return response from my csv as List of domain objects. and I have the following error Cannot deserialize instance of com.example.million.model.Domain out of START_ARRAY token

My code is the following:

@Service
class DomainService {
    fun getDomains(): List<Domain> {


        val mapper = CsvMapper()
        mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY)
        val csvFile = File("myCsv.csv")
        val response: List<Domain> = mapper.readerFor(Domain::class.java).readValues<Domain>(csvFile).readAll()
        return response
    }
}

data class Domains(var domain: String){}

标签: springlistcsvkotlinjackson

解决方案


Deleting this line, as otherwise you are wrapping each csv line in an array, which leads to your error message.

mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY)

Your code otherwise looks fine.


推荐阅读