jackson serialize csv property order

user2598799 picture user2598799 · Apr 29, 2016 · Viewed 7.5k times · Source

we have a table with 350+ columns. pojo class is generated and getters order get messed up. trying to use csvmapper from jackson, but it generates csv based on getter order. @JsonPropertyOrder is also not use feasible because of many columns.we maintain column ordering in xml and can generate field order array at runtime. can we override at runtime to provide array of fieldnames for property ordering? can we customize using annotation introspector?

Answer

Vladyslav Baidak picture Vladyslav Baidak · Feb 20, 2019

What you are looking for is called a MappingFeature. You need to disable alphanumeric sorting of properties, which is enabled by default:

CsvMapper mapper = new CsvMapper();
mapper.disable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);

More on this you can find here: Add a feature in CsvSchema to allow definition of ordering #42