spring - 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){}
解决方案
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.
推荐阅读
- tomcat - web-app 无法与使用 windows odbc 数据源管理员配置的 64 位数据库建立任何连接
- blender - 如何将 USDC 转换为 OBJ
- python - Django - 使用旧字段的值迁移新字段
- rust - 如何在 rust 中找到本地时区偏移量
- python - 我想使用来自网站的 python 解析来自多个 json+id 标签的数据
- html - 将新表行添加到现有表但在第一行之前
- windows - 视窗10 | Powershell 重定向到 Microsoft 商店
- sql - 基于其他选定行中的最大值的一列的 SQL 值
- python - 将 json 插入 postgress 数据库 - 看到 TypeError:列表索引必须是整数或切片,而不是列表
- python - 从导入的文件调用 __main__ 文件中的函数