jackson-dataformat-csv: cannot serialize LocalDate

橙三吉。 提交于 2020-01-24 14:34:30

问题


When I try to serialize object containing Local date, I get following error:

csv generator does not support object values for properties

I have JSR-310 module enabled, with WRITE_DATES_AS_TIMESTAMPS and I can convert the same object to JSON without problem.

For now I resorted to mapping the object to another, string only object, but it's decadent and wasteful.

Is there a way for Jackson csv mapper to acknowledge localDates? Should I somehow enable JSR-310 specifically for csv mapper?


回答1:


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.

No additional annotations needed in Pojo class.



来源:https://stackoverflow.com/questions/44152765/jackson-dataformat-csv-cannot-serialize-localdate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!