jackson-dataformat-csv:无法序列化LocalDate [英] jackson-dataformat-csv: cannot serialize LocalDate
问题描述
当我尝试序列化包含本地日期的对象时,出现以下错误:
When I try to serialize object containing Local date, I get following error:
csv生成器不支持属性的对象值
csv generator does not support object values for properties
我启用了WRITE_DATES_AS_TIMESTAMPS
的JSR-310模块,并且可以毫无问题地将同一对象转换为JSON.
I have JSR-310 module enabled, with WRITE_DATES_AS_TIMESTAMPS
and I can convert the same object to JSON without problem.
目前,我诉诸于将对象映射到另一个仅字符串对象,但这是s废和浪费的.
For now I resorted to mapping the object to another, string only object, but it's decadent and wasteful.
Jackson csv映射器是否可以确认localDates?我是否应该以某种方式启用专门用于csv映射器的JSR-310?
Is there a way for Jackson csv mapper to acknowledge localDates? Should I somehow enable JSR-310 specifically for csv mapper?
推荐答案
由于在架构之后配置了映射器,因此我遇到了同样的问题.确保您使用的是杰克逊及其模块的最新版本.这段代码对我有用:
I had the same problem because of configuring mapper after schema. Make sure you are using the latest verson of jackson and its modules. This code works for me:
final CsvMapper mapper = new CsvMapper();
mapper.findAndRegisterModules();
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); //Optional
final CsvSchema schema = mapper.schemaFor(PojoWithLocalDate.class);
// Use this mapper and schema as you need to: get readers, writers etc.
Pojo类中不需要其他注释.
No additional annotations needed in Pojo class.
这篇关于jackson-dataformat-csv:无法序列化LocalDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!