I\'m using Spring Batch to extract a CSV file from a DB table which has a mix of column types. The sample table SQL schema is
[product] [varchar](16) NOT NULL,
You can
BigDecimalToStringConverter implements Converter to format big decimal without trailing 0'sConversionService (MyConversionService) and register into the custom converterDelimitedLineAggregator, inject MyConversionService, override doAggregate() to format fields using injected conversion servicepublic class MyConversionService extends DefaultConversionService {
public MyConversionService() {
super();
addConverter(new BigDecimalToStringConverter());
}
}
public class MyFieldLineAggregator extends DelimitedLineAggregator {
private ConversionService cs = new MyConversionService();
public String doAggregate(Object[] fields) {
for(int i = 0;i < fields.length;i++) {
final Object o = fields[i];
if(cs.canConvert(o.getClass(), String.class)) {
fields[i] = cs.convert(o, String.class);
}
}
return super.doAggregate(fields);
}
}