Is it possible to define custom header names when serializing a POJO into CSV.
In other words, if I have a field named someField
in my PoJO, I would like the header column in output CSV file to be named Some custom field name
for example.
Thanks.
It's possible with a use of mixins, since you want to use those name only for csv export:
Let assume you have id
field in your Pojo class with a getter. Then you Create PojoFormat abstract class:
public abstract class PojoFormat {
@JsonProperty("Report Id")
abstract Integer getId();
}
And in your code use it like that:
CsvMapper mapper = new CsvMapper();
mapper.addMixIn(Pojo.class, PojoFormat.class);
CsvSchema schema = mapper.schemaFor(Pojo.class).withHeader();
mapper.writer(schema).writeValueAsString(objects);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With